1
votes

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");
2
Where have you placed your shortcode function? Is this inside your theme's functions.php file? You mention this is inside the text editor of a WordPress post so the filters you're showing don't apply. Which file are you outputting this in? Can you include your template markup for this particular section?Nathan Dawson
@NathanDawson I have added that shortcode function inside functions.php of theme and the shortcode [template_url] i have used inside the post text editor while creating a post.Yaser Nadeem
Please include the contents of single.phpNathan Dawson
@NathanDawson Thanks for your hint we got the problem i will add that in the answer.Yaser Nadeem

2 Answers

2
votes

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.

1
votes

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.