WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In WordPress shortcodes, how can I pass boolean attributes?
Both of [shortcode boolean_attribute="true"] or [shortcode boolean_attribute=true] are giving string values.


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?

share|improve this question
Simply don't pass the attribute and you'll have false, otherwise true. – brasofilo Oct 18 '13 at 10:33
Thanks @brasofilo. But is there any other solution? I think some users will get lost if they give an attribute "false" value, but they will get "true" value. – Sodbileg Gansukh Oct 18 '13 at 10:35
up vote 7 down vote accepted

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']


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;
share|improve this answer
Do not use extract(). :) – toscho Oct 18 '13 at 10:53
That's great answer. But I thought it would be great if users had true or false choice. Sorry I cannot accept your answer as accepted. – Sodbileg Gansukh Oct 18 '13 at 10:53
@SodbilegGansukh this code works also with 'true' and 'false' – gmazzap Oct 18 '13 at 10:56
@toscho I just copied and pasted from codex ;) – gmazzap Oct 18 '13 at 10:57
@SodbilegGansukh All arguments are passed as strings, because they can be entered (typed) as strings only. There is no other way than this answer. Try to type a stringless boolean value … – toscho Oct 18 '13 at 10:59

As an extension to @G.M. answer (which is the only possible way to do this), here's a slightly shortened/beautified and and an extended version (which I personally prefer):

Shortened/Beautified variant

It's enough to do a boolean check for the contained value. If it's true, the result will be (bool) true, else it will be false. This produces a one case true, everything else false result.

add_shortcode( 'shortcodeWPSE', 'wpse119294ShortcodeCbA' );
function wpse119294ShortcodeCbA( $atts ) {
    $args = shortcode_atts( array(
        'boolAttr' => 'true'
    ), $atts, 'shortcodeWPSE' );

    $args['boolAttr'] = 'true' === $args['boolAttr'];

Extended/User-safe variant

The reason why I prefer this version is that it allows the user to type in on/yes/1 as an alias for true. This reduces the chance for user errors when the user doesn't remember what the actual value for true was.

add_shortcode( 'shortcodeWPSE', 'wpse119294ShortcodeCbA' );
function wpse119294ShortcodeCbA( $atts ) {
    $args = shortcode_atts( array(
        'boolAttr' => 'true'
    ), $atts, 'shortcodeWPSE' );

    $args['boolAttr'] = filter_var( $args['boolAttr'], FILTER_VALIDATE_BOOLEAN );

Additional notes:

1) Always pass the 3rd argument for shortcode_atts(). Else the shortcode attributes filter is impossible to target.

// The var in the filter name refers to the 3rd argument.
apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );

2) Never use extract(). Even core wants to reduce those calls. It's equally worse to global variables, as IDEs don't stand a chance to resolve the extracted contents and will throw failure messages.

share|improve this answer
+1 great answer – gmazzap Oct 18 '13 at 11:14
Wow, that's a great answer. Thank you so much. I wish I had enough reputation to vote up for your answer. And thank you so much for your advice. – Sodbileg Gansukh Oct 18 '13 at 11:16
I voted on your behalf, Sodbileg. :) – Dero Oct 18 '13 at 11:50
Thanks for that @Dero. :) – Sodbileg Gansukh Oct 18 '13 at 14:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.