Pass boolean value in shortcode

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;
}

Leave a Comment