0

I'm looking for a way to check just check for an option existing when executing a shortcode, but that option doesnt need a value. How do I just check for JUST an option? For example I tried this out, I want the following code to output true, but it is still outputting false.

PHP:

function Test($atts) {
 extract(shortcode_atts(array(
  'row' => null
 ), $atts));
 $output = 'false';
 if($row) {
  $output = 'true';
 }
 return $output;
}
add_shortcode("test", "Test");

In The WYSIWYG:

[test row]

Will just output "false". But if I were to say:

[test row="true"]

It will output "true" onto the page.

Again, I want to just check for that option row just being there, not to have to feed it a value. Thanks!

3

It seems that extract() function sets it to false if value is not provided. There might be a better way around it, but I've used workaround as follows

function Test($atts) {
    extract(shortcode_atts(array(
     'row' => null
    ), $atts));

    $output = 'false';
    if( in_array('row', $atts))
        $output = 'true';

    return $output;
}
add_shortcode("test", "Test");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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