In WordPress shortcodes, how can I pass boolean attributes?
Both of [shortcode boolean_attribute="true"]
or [shortcode boolean_attribute=true]
are giving string values.
EDIT
There would be no problem for users who know what they’re doing if I use the trick which was commented by @brasofilo. But some users will get lost if they give an attribute false
value and receive true
value. So is there any other solution?
Is easy to use 0
and 1
values and then typecasting inside the function:
[shortcode boolean_attribute="1"]
or [shortcode boolean_attribute="0"]
but if you want you can also strictly check for 'false'
and assign it to boolean, in this way you can also use:
[shortcode boolean_attribute="false"]
or [shortcode boolean_attribute="true"]
Then:
add_shortcode( 'shortcode', 'shortcode_cb' );
function shortcode_cb( $atts ) {
extract( shortcode_atts( array(
'boolean_attribute' => 1
), $atts ) );
if ( $boolean_attribute === 'false' ) $boolean_attribute = false; // just to be sure...
$boolean_attribute = (bool) $boolean_attribute;
}