Shortcodes in Widgets for Plugin Authors

Just overcome something which could have turned out to be a nightmare thanks to this guy.

So a plugin I’m currently working on uses Widgets which need to be able to support shortcode output.

In the legacy version I was using custom post types for the widget content and outputting that using the the_content() function worked fine provided I had added:

  • add_filter( ‘widget_text’, ‘shortcode_unautop’);
  • add_filter(‘widget_text’, ‘do_shortcode’, 11);

So far so good. Until I started using Post Meta data instead of custom posts this meant my widget included output similar to this:

<?php

$myvar = “hello [myshortcode]”;
echo $myvar;

?>

This was rendering as just a string on the screen reading – “hello [myshortcode]”. Bummer!

Anyway the fix is simple.

<?php

$myvar = “hello [myshortcode]”;
$myvar = do_shortcode($myvar);
echo $myvar;

?>

Easy when you know how right?

Advertisements
Posted in wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: