• I am struggling to understand how do_shortcode works and the results I am getting. Hopefully someone can help.
    I am trying to get the value of a shortcode into a php variable so I can use it in an IF statement.
    So I have a statement:
    $raceno=do_shortcode(‘#_ATT{Race}’};
    If I then echo $raceno then the output is for example “17”.
    Yet if I compare this variable with “17” it is not equal!
    If I do a strlen($raceno) then it is 11! This is the length of the shortcode name.
    So I have a variable which if displayed is 17 but if compared is the string #_ATT{Race}.
    How does that work! And how can I actually get a php variable with the actual value so I can do compares etc.

    Can anyone explain?

Viewing 9 replies - 1 through 9 (of 9 total)
  • One little gotcha with do_shortcode() is that all the function does is search whatever content for shortcodes and return the same content, but with the shortcodes filtered out. You need to call do_shortcode with the brackets around the shortcode or something unexpected will happen:

    $foo = do_shortcode( bar ); gets you “bar” back, but $foo = do_shortcode( '[bar]' ); gets you whatever the shortcode [bar] returns.

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Not sure that explains it or solves my problem.
    do_shortcode(‘[#_ATT{Race}]’); produces an echo of [17] and a length of 15
    do_shortcode(‘#_ATT{Race}’); produces an echo of 17 and a length of 13

    How do I get an echo of 17 and length of 2?

    Are you using a plugin to generate the shortcode #_ATT{Race}? Can you show the function that’s hooked to that shortcode? (If it’s really long, post it somewhere like Pastebin or Gist; those services work better for really long blocks of code.)

    Thread Starter bobjgarrett

    (@bobjgarrett)

    This is a shortened form of the page as I try to get the full page working:
    <?php
    global $wpdb;
    ?>
    [events_list year=”2017″]
    Race No. #_ATT{Race}
    #_EVENTDATES
    #_EVENTNAME
    <?php
    echo “Race No. as variable=”;
    $raceno = do_shortcode(‘#_ATT{Race}’);
    $newstring = ‘X’ . $raceno . ‘X’;
    echo $newstring . ‘ length=’ . strlen($newstring);
    ?>
    <hr>
    [/events_list]

    Are you using a plugin for the [events_list] shortcode? How are #_ATT{}, #_EVENTDATES, and #_EVENTNAME defined?

    Thread Starter bobjgarrett

    (@bobjgarrett)

    They are created by the plugin “Events Manager”, whose authors have been unable to explain the strange behaviour of the do_shortcode function.

    Now that I know more about the situation, I think we were going down the wrong path. What is the output of var_dump( $raceno ) or print_r( $raceno )?

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Strangely neither var_dump nor print_r produce any result.

    Code is now:
    echo “var_dump=” . var_dump($raceno) . “</br>”;
    echo “print_r=”;
    print_r($raceno);
    echo “</br>”;
    $raceno = do_shortcode(‘#_ATT{Race}’, $ignore_html = false);
    $newstring = ‘X’ . $raceno . ‘X’;
    echo “raceno=” . $raceno . ‘ length=’ . strlen($raceno);

    and produces:
    Race No. 01
    08/04/2017
    Fitting Out Supper and Race 1
    NULL
    var_dump=
    print_r=
    raceno=01 length=11

    Thread Starter bobjgarrett

    (@bobjgarrett)

    My mistake in the code order.
    var_dump produces string(11) “01”
    but print_r produces 01

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘do_shortcode not understood’ is closed to new replies.