do_shortcode() doesn’t work if shortcode contained in variable, works if shortcode passed as string

I’m developing a plugin, wherein one of the features is to switch out forms on a specific page by changing the shortcode within a custom field.

within my plugin file:

Read More
function getShort()
{
    global $post;
    $m = get_post_meta($post->ID, 'short', true);
    return $m;
}

within my theme file:

$short = getShort();
echo do_shortcode($short);

With the code above, it just prints out the shortcode. If I echo the value of $short, copy and paste that as the argument for do_shortcode(), it prints out the expected value.

I am 100% confident that the value of $short is a string, and is the correctly formatted shortcode. Any idea why this doesn’t work?

Related posts

Leave a Reply

2 comments

  1. Make sure that the string in $short variable is between square brakets, like [header]. If it’s not stored that way, make the call like this echo do_shortcode('['.$short.']');

  2. Even though you’re confident that this isn’t the case, it sounds as if the value you are passing manually is not the same as the value in $short.

    Dump the contents of $short (var_dump($short);) and have a look the source of the genereated page! There’s a good chance that the variable contains tags or other chars that won’t be visible in the rendered browser view.