WordPress.org

Support

Support » Plugins and Hacks » Shortcode inside widget not working correctly

Shortcode inside widget not working correctly

  • Hi there,

    I’ve created a shortcode I’d like to use in a widget, however it’s behaving a bit wonky and I can’t figure it out. Here’s the code in functions.php:

    function related_content( $atts ) {
    	return get_the_ID();
    }
    add_shortcode('related-content', 'related_content');

    The problem is that if I embed this shortcode in a page, it does indeed spit out the correct ID of that page. However if I embed the code in a sidebar widget, it spits out a different ID entirely (it happens to be the ID of my blog page in this case). Any thoughts on why this is happening? Is there something about the context in which the shortcode is being called inside a widget? Any ideas?

    Thanks!
    – Mickey

Viewing 1 replies (of 1 total)
  • Bozz

    @bozzmedia

    Try this in your functions.php:

    add_filter('widget_text', 'do_shortcode');

    This wasn’t an issue for me on WordPress 3.5. In the upgrade to 3.6/3.7 I had to set this up for some reason. Hope that helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Shortcode inside widget not working correctly’ is closed to new replies.
Skip to toolbar