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
}