Sillybean
using shortcodes everywhere
Read this in Spanish | Leer esta en Español
At the moment, shortcodes in WordPress are processed only in post/page content. You can use them in lots of other places, though, if you enable them for each field you want. Here’s how to use shortcodes in widgets, excerpts, comments, theme files, user descriptions, and category/tag/taxonomy descriptions.
Text Widgets
It’s easy to make shortcodes work in text widgets. Just add the following to your functions.php file:
add_filter( 'widget_text', 'shortcode_unautop'); add_filter( 'widget_text', 'do_shortcode');
The second line is the one that makes the shortcodes work, but you’ll want to include both. If you check “add paragraphs automatically” on the widget, WordPress will apply the autop filter — the one that turns your line breaks into paragraph and break tags. If a shortcode is on its own line, it would normally get wrapped in a paragraph tag. The first line prevents that from happening.
Template Files
You can use shortcodes right in your theme! Use the do_shortcode() function, where the argument is a string that contains a shortcode.
For example, to print the output of the shortcode [foo] in a theme, add this to the file:
<?php do_shortcode('[foo]'); ?>
The do_shortcode() function accepts any text as its input. If the string contains a shortcode, that code will get processed. So, for example, you could manually process your post content for shortcodes like this:
<?php $content = get_the_content(); echo do_shortcode($content); ?>
Comments
Do you trust your commenters enough to allow them to use shortcodes? This goes into functions.php:
add_filter( 'comment_text', 'shortcode_unautop'); add_filter( 'comment_text', 'do_shortcode' );
Excerpts
To enable shortcodes in excerpts, add these lines to your functions.php file:
add_filter( 'the_excerpt', 'shortcode_unautop'); add_filter( 'the_excerpt', 'do_shortcode');
User Descriptions
There is no filter (as far as I know) for the user description, so in order to display the description with shortcodes, you’ll need to fetch the description string, then pass it to the do_shortcode() function. This would go into your theme file:
<?php // $user_id = 3; $userdata = get_userdata($user_id); echo do_shortcode($userdata->description); ?>
Category, Tag, and Taxonomy Descriptions
These descriptions can be filtered, so the code for your functions.php file is simple:
add_filter( 'term_description', 'shortcode_unautop'); add_filter( 'term_description', 'do_shortcode' );
Posted on February 13, 2010 at 10:48 am in WordPress · 42 comments
Sharing this post? The short URL is: http://sillybean.net/?p=2719




Does not work on 2.9.2 at least.
I just tested again in 2.9.2 and it’s working beautifully. Which one did not work for you?
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.
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!
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.phpfiles don’t have much HTML, but you’re right, all these code snippets should go in the PHP sections.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?
Wow, This is very nice post.
I needed these codes as i am pretty new to wordpress and it will make my life easier.
Thanks for this
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?
Ryan, shortcodes rely on the WordPress backend, so there’s no easy way ton use them outside WP. Sorry!
Steph, I found the solution: require_once(‘blog/wp-blog-header.php’); This loads the WordPress enviornment and all the shortcodes anywhere.
sorry forget to check notify!
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'); ?>Not working for me in 3.0.1
Which part is not working, Asif?
turns out there was a tiny bit of code I needed in my footer file.
I needed this little bit of php before the close of my tag.
<code
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…
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.
I’m looking into the problem with excerpts in 3.0.1. Stay tuned!
Thank you very much,
It really helped :)
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_filtermethod but it just wasn’t working for me.Thankfully one of those posts linked to yours and writing the
do_shortcodedirectly into the widget function got it working.I found out what’s happening with excerpts in 3.0+. All shortcodes are stripped from the text before the excerpt is generated. It looks like the Advanced Excerpt plugin can work around this, though, so give it a try if you need shortcodes in excerpts.
I’m still looking into the problem with text widgets.
Not working in WordPress 3.01 for me either. Text widgets just outputs the shortcode tags instead of processing it.
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.
Thanks for this! I’ve been struggling most of the morning with the get_the_content() and this solved my grief. Chris
Thanks! Exactly what I needed. Worked like a charm on WP 3.0.4
Thanks so much for this, I was looking for a way to put a theme shortcode in my custom widget and modifying your text widget example did just the trick!
Hi,
How can i create a short code for widget?
i found this code
http://digwp.com/2010/04/call-widget-with-shortcode/
but in this line that should be inserted in the html page
[widget widget_name="Your_Custom_Widget"]
what is the name of the widget?
for example Recent posts widget
is the name is recent posts or something else?
Thanks!
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
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.
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!
Oh my! SO SIMPLE! Thank you so much! I tried about 3 other plugins that didn’t work… this was a simple two line code edit!
Thanks again!
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!
Working great for my wordpress blog. thanks for small code.
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.
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.
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
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
thanks so much, this code very helps me:
add_filter( 'comment_text', 'shortcode_unautop');
add_filter( 'comment_text', 'do_shortcode' );
For the excerpt you need to replace
the_excerpt()
with
echo do_shortcode(wpautop(wptexturize(get_the_excerpt())))
in your theme loops