Question

I wanted to add shortcode from the text editor of wordpress post.I have added following short code in the post of wordpress:

<img alt="" src="[template_url]/images/ic_sol_1a.png" />

Here [template_url] is the shortcode i wanted to use it was not working. when i see it in the post page it render the text not the short code response. Somewhere i saw a solutions like adding following line to functions.php of theme:

add_filter( 'widget_text', 'shortcode_unautop');

add_filter( 'widget_text', 'do_shortcode');

But still after adding these lines i am unable to make shortcode work. What could be the possible reason?

my shortcode function is like this:

function my_template_url() {
  return get_bloginfo('template_url'); 
}
add_shortcode("template_url", "my_template_url");
Was it helpful?

Solution 2

After lots of headache and with the help of @doublesharp and @Nathan Dawson comments/answers we figured out Problem was inside single.php file of theme we were getting the content of post using get_the_content function. By changing it to the_content() function sortcodes start working. Please note that shortcode will not work in the visual editor of WordPress post on admin site but when you see the post actually in the browser by going to that post you can see it working.

OTHER TIPS

You will need to use the get_bloginfo() to return the value as in your example and make sure to use the_content() to print the value to the page. If you use one of the API methods such as get_the_content() then the do_shortcode filter is not run. If you want to apply it use the following:

function my_template_url() {
    // This will echo the 'template_url'
    return get_bloginfo('template_url'); 
}
add_shortcode("template_url", "my_template_url");

$shortcoded = apply_filters( 'the_content', get_the_content() );

You do not need the add_filter() lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top