68 Comments

  1. I think that the shortcode_unautop has been deprecated under 2.9.2 which could cause problems.

    Also, I thought that changing the functions.php file, while easy, can lead to problems if you change themes or get an updated version of a theme that overwrites the functions.php.

    I created a simple plugin to allow the widget shortcodes that should work with any theme. It also checks for the existence of shortcode_unautop before hooking it in.

    You could modify the plugin to add functionality for shortcodes in other areas as needed (I currently only need it for widgets) by adding the appropriate add_filter calls.

    http://blogs.wcnickerson.ca/wordpress/plugins/widgetshortcodes/ is where you can find it.

    • It hasn’t been deprecated as of today’s trunk. It was just introduced in 2.9.0, so I think it’ll be around a while.

      • Ah! That explains it. I haven’t upgraded the version on my PC for a while and it is older than 2.9.0. Probably a good thing since it forced me to add the check for existence. Now it will work okay on older setups as well as the newer ones.

        Thanks Stephanie.

  2. I’m a an amateur at all of this, so please be gentle (and simplistic) with me, thanks.

    RE: shortcode in excerpts

    Wasn’t sure which functions.php file to use (WP or theme), and no idea where to place the code, so I experimented with both. Placing it in the theme functions.php file did nothing (shortcode still showing up in excerpts as text), and when I put it in the WP functions.php file, my entire site disappeared.

    Thanks for any help!

  3. You can’t just cut-and-past the code in the article above into your WordPress functions.php file because WordPress has made the single quotes into ‘smart quotes’. This means that the quotes won’t be recognized as valid PHP code until you change them back to regular quotes. Once you do that, it should work OK.

    Also, when pasting it into your functions.php file, make sure that you paste it into a PHP area, not an HTML area, and in an area where it won’t conflict with other code that might be there.

    • Thanks for letting me know about the quotes. They got mangled in the database at some point since I posted this; I’ve fixed them all.

      Most functions.php files don’t have much HTML, but you’re right, all these code snippets should go in the PHP sections.

  4. This is a fantastic post. I’m sure it’s been very helpful for many people. I know it was for me.

    I do wonder whether it is possible to use a shortcode in a page or site title, though?

  5. Hi, I was wondering how to use shortcodes in a non wordpress site hosted on the same server. My front end is WordPress and the backend Admin panel is HTML based. My goal is to use WordPress widgets (shortcodes) on my HTML based admin panel. Is this possible?

      • Steph, I found the solution: require_once(‘blog/wp-blog-header.php’); This loads the WordPress enviornment and all the shortcodes anywhere.

  6. I can’t get it to work. I retyped the code into my functions.php file, I added it at the very beginning, but it’s still not working. Can anyone see anything wrong here, running wp 3.1.

    <?php 
    add_filter('widget_text', 'shortcode_unautop');
    add_filter('widget_text', 'do_shortcode');
    
    require_once(TEMPLATEPATH . '/epanel/custom_functions.php'); 
    
    require_once(TEMPLATEPATH . '/includes/functions/comments.php'); 
    
    require_once(TEMPLATEPATH . '/includes/functions/sidebars.php'); 
    
    load_theme_textdomain('Professional',get_template_directory().'/lang');
    
    require_once(TEMPLATEPATH . '/epanel/options_professional.php');
    
    require_once(TEMPLATEPATH . '/epanel/core_functions.php'); 
    
    require_once(TEMPLATEPATH . '/epanel/post_thumbnails_professional.php');
    
    function register_main_menus() {
    	register_nav_menus(
    		array(
    			'primary-menu' => __( 'Primary Menu' )
    		)
    	);
    };
    if (function_exists('register_nav_menus')) add_action( 'init', 'register_main_menus' );
    
    $wp_ver = substr($GLOBALS['wp_version'],0,3);
    if ($wp_ver >= 2.8) include(TEMPLATEPATH . '/includes/widgets.php'); ?>
  7. I’m running 3.0.1 and I can’t seem to get this to work. I want to show shortcode in my excerpts. I put the appropriate filters in my functions file, but it doesn’t do anything…

  8. It’s interesting, Stephanie. In 3.01, adding this to my functions.php file …

    add_filter(‘widget_text’, ‘do_shortcode’);

    … is not enabling shortcodes in text widgets for me, either.

  9. Thanks for this article. I’d been trying to put shortcode into a widget I’d written to allow custom sidebar text on a per post basis and was not having any luck with it.

    Google gave me a heap of posts using the add_filter method but it just wasn’t working for me.

    Thankfully one of those posts linked to yours and writing the do_shortcode directly into the widget function got it working.

  10. It is nice there’s a plugin for getting around stripping of shortcodes in excerpts. However, if you’re building a theme a short and sweet method would be a better way to go. Currently, using a dropcap shortcode actually removes the entire first letter of the post along with the shortcode.

  11. I am using WP 3.5 Latest Twenty Ten theme.
    Advanced Excerpt
    Excerpt Editor
    WP PostRatings

    I use Excerpt editor so I can display different content on the blog page/archive etc. then when someone clicks the title of the post, it brings them to the posts individual page. and displays different content (slightly different)

    THe problem is, on the blog page,/archive etc.. the short code for Wp postratings is no longer displaying the ratings for that post. its just the textual short code. i used the two lines of code above and cant seem to get it to work.
    any idea?
    Thanks

  12. I am also running into an issue with text widget output. It’s simply returning the shortcode as a string. I tried it also with ‘widget_execphp’ with no luck. Running 3.0.5

    Bummer.

  13. Hi Stephanie,

    I just ran across your post while have been searching for a way to run a shortcode within the output of another shortcode.

    I’m using the q and a faq plugin by raygun. Some of my faq answer items need to have tables for aesthetically pleasing output.

    I initially tried inserting the 2 lines of code into the php file of the q and plugin, but no result.

    Do you know if running a shortcode within the output of another shortcode is possible? If so, how would I go about getting it to work?

    Thanks for any help you can provide!
    Steph

    • the developer just released an update to the plugin that allows shortcodes in the output of the q and a faq plugin shortcode!

  14. Hi, Using Version 3.2.1, I just pasted the shortcode into the functions.php right before the closing ?> tag and the text widget simple outputs the shortcode and not the form it’s calling.

    Here’s the code I pasted.

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

    Is there something new I need to know?

    Thanks!

  15. Thanks, but doesn’t work for me when using amazon-tools plugin in the custom excerpt box.

    The advanced excerpt plug-in does work on my site though.

  16. Hi – thanks for the inspiration that enabled me to find a way to add shortcodes where they can enhance my sites- specifically in sidebar widgets.

    I am not that technically minded, but sometimes a different approach (from naivety) works.

    I simply :
    >> added shortcodes to a TEXT widget
    >> added breaks between lines
    >> enabled “auto add paragraphs”

    It worked and is so much easier that coding up
    Please note it worked on ALL but ONE of my sites I have tried it on
    but 11/12 is a good result and worth trying out –
    See examples on the following pages ( not set to appear on all pages yet – as I am using Widget Logic to put widget at top of only certain pages listed below – and shortcodes help HIGHLIGHT the links to my desired outcome ) _ I am also still finishing off the site…….

    (Again- thanks for the inspiration and hope some of your readers find this useful )

    Spell Night – Ultimate Girls Night In
    Spell Night – Night of Magic
    Spell Night – Celebrate Friendship
    Hosting a Spell Night

  17. Hi,
    Im trying to get short-codes to work on excerpts
    I tried adding this add_filter to the php file of the theme and nothing happened. I tried adding to WordPress php file, and got that add_filter is not a recognized function.
    I added the advanced excerpt and unchecked the option to remove short-codes from excerpt and still can’t get them to work.
    Any more ideas?? I have the latest version of WordPress in a multisite environment, but the advance plugin is only active on one site.
    Thanks

    • I’m having trouble getting the columns to pull up using that code for my excerpts. It’s all stacked to the left. Can you help me out?

  18. I’ve found a quick way to get shortcodes working for all excerpts whether they are manually or automatically generated. Basically you have to unregister WordPress’ core filter that handles the generation of the excerpt and register your own modified version.

    You can read the detailed explanation and sample code on my blog

  19. so how can I get short codes to work in another plugin? I want to get the rokbox light box function working in the wp-download manager template area.

  20. This is awesome. Thanks for this. I think I am so close to figuring out what is going on with me. I have a custom field I am using with shortcodes in it that I would like to run. I thought perhaps

    add_filter( ‘get_post_meta’, ‘shortcode_unautop’);
    add_filter( ‘get_post_meta’, ‘do_shortcode’);

    But no luck so far. Anyone have any ideas how I might run shortcode in a custom field. do_shortcode will not work because there is other content in the field.

    • Hey, Jeff. Adding a filter won’t work because the standard filters that are applied to the_title, the_content, etc. aren’t applied to custom fields. What you need to do is more like this:

      global $post;
      $content = get_post_meta( $post->ID, 'my_custom_field', true );
      echo do_shortcode( $content );
      
  21. Stephanie: how do I contact you for consulting work? The Quote page has no form and the Support Forum link does not exist? Are you still doing custom projects? Thanks!

    • How are you trying to make it work in the footer? Text filters aren’t applied to theme files, so unless there’s a widget in your footer, you’ll need to run the shortcode directly on the content in question, just like I showed Jeff above.

  22. Hey Steph,

    Thanks for the quick guide.

    I am actually using private blog plugin, that allows me to set up to 10 passwords for my users.

    I won’t to add a shortcode on the welcome page (template is in html). How to make it work? Thanks in advance.

  23. Hi Stephanie, thanks for so much information. I am new to this, but I am hoping you can help me. Do you know why my shortcode is only producing the first word in a couple word meta key? I created the shortcode using array to allow for attributes in a href. It works great, yet the text of the href is only kicking back the first word in the meta_key submission. I am not sure if the array is the issue or what? Any help would be appreciated

  24. Thanks a ton for this nice little guide.

    Is this suppose dto work with custom shortcodes created through extra shortcode functions or only default WordPress shortcodes?

    I tried the sidebar widget text thing in a multisite install which has some custom shortcodes added to it. The custom shortcodes were created through a function we had developed.

    Haven’t managed to get them to execute in text widgets with this function. Anybody used this successfully on multisite with custom shortcodes?

  25. OMG!
    Thank you!!!!!!!!!!!

    I killed a day to make it work for category_description,

    I tried
    add_filter( ‘category_description’, ‘do_shortcode’ );
    but it doesn’t work.

    Your solution
    add_filter( ‘term_description’, ‘shortcode_unautop’);
    add_filter( ‘term_description’, ‘do_shortcode’ );
    worked perfectly!!!

    thankyou-thankyou-thankyou!

Leave a Reply

Required fields are marked *.