最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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:

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?

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:

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?

Share Improve this question asked Oct 17, 2012 at 0:20 PizaulPizaul 315 bronze badges 1
  • have you tried to add esc_attr to $m, like return esc_attr( $m );? – Mike Madern Commented Jan 23, 2013 at 10:08
Add a comment  | 

2 Answers 2

Reset to default 2

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.']');

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论