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.
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.)
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?
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 )?
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
My mistake in the code order.
var_dump produces string(11) “01”
but print_r produces 01