admin管理员组文章数量:1323023
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 11 Answer
Reset to default 0Yes, 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
}
本文标签: plugin developmentWordpress shortcode with a switch
版权声明:本文标题:plugin development - Wordpress shortcode with a switch 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742110506a2421232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论