80 thoughts on “using shortcodes everywhere

  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.

    • steph

      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.

    • steph

      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. Ryan

    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?

  6. Jan

    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. Jen

    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. stephanie

    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

    • stephanie

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

  14. Mitch

    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. Cris

    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

    • Amelia

      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. michael Ramirez

    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. Jeff

    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. Divye

    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. Ilya

    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!

  26. Old topic but still relevant… A lot of people struggle with this and do not understand why shortcodes does not work in widgets. It can also be done in code using $output = do_shortcode($output); to ensure the output is checked for shortcodes to be executed!

  27. Anes P.A

    Hi Stephanie,
    It’s a fantastic post . Thanks alot .
    I need 1 more information , do we can access the shortcode logic from outside the WordPress.
    I am planning to write a REST API which provide the service for non-wp site . If the short code
    stuff can access by the outside API , it’s easy to implement it . Please advise me , I am novice in WP.

    Thanks
    Anes

  28. RayS

    Have a shortcode that works in widgets (plugin latestbyauthor) using syntax of:
    [latestbyauthor author="authorname" show="nn"]
    (nn defaults to 10)

    Would like to build a page with a search form whose argument would be passed as the ‘author’ keyword to create a page of links to the searched-for author’s posts.

    Done a lot of work with forms via javascript and VBscript but rather new to PHP and can’t figure out how to get the argument to the php echo command

  29. Just used your advice for a client’s Woocommerce site. Needed to display PDF parts lists in a category description. And now I can! Thank you soooo much.

    I was unable to use various PDF embed or viewer plugins because shortcode wasn’t working in the description. I used your “do_shortcode($content)” and it worked like a charm!
    Of course I had to figure out how to include that in the code in my theme, but got it on the first try.

    In case it helps anyone else, my exact line of code is now:
    <?php echo '’ . do_shortcode( wpautop( wptexturize( term_description() ) ) ) . ”; ?>

    That may be theme specific, but other Woocommerce themes would be something similar. Thanks again!

  30. Pamela

    Hello,
    I put these two lines in the function.php to execute a shortcode in a text widget, which is in the featured area of my template.
    add_filter( ‘widget_text’, ‘shortcode_unautop’);
    add_filter( ‘widget_text’, ‘do_shortcode’);

    The text displays in the widget, but for some reason, the two columns of the template that are supposed to be under the featured area (where shortcode has been executed) are pushed to the bottom of the page.

    Is this ‘shortcode_unautop’ causing that to happen?

    Thanks for a helpful article.

    pam

  31. Victor

    Hi, can tell me somebody how can I make shortcodes working in the header area, respectively in the title meta. I use a wp theme in which I add a shortcode in the title field – it works fine in the content area of the page, but in the header meta doesn’t show up the expected value but just the code – like [shortcode]. Any solution for this, maybe with an add_filter in the functions.php?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>