最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Wordpress shortcode with a switch

programmeradmin4浏览0评论

I am developing a plugin and have a shortcode where some info is added. this is called by [info] I would like to be able to add an 'extended' param as a switch so that i can have extended info if i set this [info] would show short info and [info extended] would show extended with something like

   function info($atts){
      extract(shortcode_atts(array(
            'extended' => '',
            ), $atts));
     ob_start();
        echo 'info';
        if (isset($extended)){
            echo 'More info';
            }
      return ob_get_clean();
    }

but can not find how to do this. I only find options where i must add value to the param and check this like [info extended="full"] and then check the value of the param. Anyone got some pointers?

I am developing a plugin and have a shortcode where some info is added. this is called by [info] I would like to be able to add an 'extended' param as a switch so that i can have extended info if i set this [info] would show short info and [info extended] would show extended with something like

   function info($atts){
      extract(shortcode_atts(array(
            'extended' => '',
            ), $atts));
     ob_start();
        echo 'info';
        if (isset($extended)){
            echo 'More info';
            }
      return ob_get_clean();
    }

but can not find how to do this. I only find options where i must add value to the param and check this like [info extended="full"] and then check the value of the param. Anyone got some pointers?

Share Improve this question asked Sep 15, 2020 at 7:55 YgdrazilYgdrazil 1
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can pass shortcode parameters wihtout values. They just appear on $atts with numeric keys. You can see this when you do var_dump($atts). One option to check their existance is then to use in_array().

function info( $atts ) {
    // check if extended is passed in $atts with or without value
    $extended = isset( $atts['extended'] ) ? $atts['extended'] : in_array( 'extended', $atts );

    if ( $extended ) {
        // do something
    }

    // more code
}
发布评论

评论列表(0)

  1. 暂无评论