Read this in Spanish | Leer esta en Español
Read this in Turkish | Türkçe bunu okuyun
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' );
norrige dalla says
Does not work on 2.9.2 at least.
Stephanie says
I just tested again in 2.9.2 and it’s working beautifully. Which one did not work for you?
LoneWolf says
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 says
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.
LoneWolf says
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.
Russell says
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!
Frank C says
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 says
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.Nick says
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?
Ali says
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
Ryan says
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 says
Ryan, shortcodes rely on the WordPress backend, so there’s no easy way ton use them outside WP. Sorry!
Ryan says
Steph, I found the solution: require_once(‘blog/wp-blog-header.php’); This loads the WordPress enviornment and all the shortcodes anywhere.
Ryan says
sorry forget to check notify!
Jan says
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.
asif says
Not working for me in 3.0.1
steph says
Which part is not working, Asif?
Jan says
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
Rob says
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…
Jay Collier says
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.
steph says
I’m looking into the problem with excerpts in 3.0.1. Stay tuned!
XYDAC says
Thank you very much,
It really helped :)
Stuart says
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.Stephanie says
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.
David Gwyer says
Not working in WordPress 3.01 for me either. Text widgets just outputs the shortcode tags instead of processing it.
Jen says
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.
Christopher Ross says
Thanks for this! I’ve been struggling most of the morning with the get_the_content() and this solved my grief. Chris
Martin says
Thanks! Exactly what I needed. Worked like a charm on WP 3.0.4
Montana Flynn says
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!
roy says
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!
chris says
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
Bryan says
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.
stephanie says
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 says
the developer just released an update to the plugin that allows shortcodes in the output of the q and a faq plugin shortcode!
Veloz Skate Company says
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!
Mitch says
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!
karthimx says
Working great for my wordpress blog. thanks for small code.
Adam says
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.
John Newell says
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
Cris says
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
msrosyidi says
thanks so much, this code very helps me:
add_filter( 'comment_text', 'shortcode_unautop');
add_filter( 'comment_text', 'do_shortcode' );
Andrew Benbow says
For the excerpt you need to replace
the_excerpt()
with
echo do_shortcode(wpautop(wptexturize(get_the_excerpt())))
in your theme loops
Amelia says
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?
Dan Imbrogno says
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
ray says
Thanks for a great tutorial Stephanie…
Very good info
8-|
Judavi says
Omg! Thanks :)
Richard says
For some reason the shortcodes in widgets isn’t working for me. I use a child theme to edit my functions.php, could that be the problem?
michael Ramirez says
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.
Rohit Manglik says
Thanks a lot for the description.. It worked for me
Miracle says
How to use do_shortcode or short code in option page or admin page?
It seems not work in menu page , option page …
Dustin says
I’m wondering this too! Trying to use a shortcode in a desktop widget
Jeff says
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.
Stephanie Leary says
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:
Jeff says
Awesome. Thanks Stephanie. You are awesome as always!! :)
Peter says
Hi All,
Which file these lines need to be put in for shortcodes to work in Custom fields?
functions.php, loop.php?
Any advise on this will be greatly appreciated,
P
Melissa says
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!
Stephanie Leary says
Oh dear. I knew some things were broken, as I’m moving hosts and domains, but the contact form is rather essential! Thanks for letting me know. I’ll email you.
jack says
I’m not able to get this working on the footer of an older theme, I guess it only works on new themes?
Stephanie Leary says
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.
web development company says
Wow, if I had known this while doing my last WordPress project, it would have made my life so much easier. Unfortunately for deadline restrictions, I didn’t have time to thoroughly research it.
Thanks for sharing!
Remco says
Super, worked! Thanks a lot))
Divye says
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.
Bob says
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
David Radovanovic says
Was looking all over for allowing shortcuts in category descriptions. Thanks for sharing.
Nur Ahmad Furlong says
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?
Merk says
Nice and simple! Have been looking, but others are not as well explained.
Thanks for this great post.
Jim says
Thanks! Worked great for category descriptions.
WPExplorer says
Just what I was looking for – dam very comprehensive. Thanks.
Ralph says
Great job! Thanks a lot!
Ilya says
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!
Lars says
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!
Anes P.A says
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
RayS says
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
Justin says
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!
maki says
Hey stephanie,
this hack still works, thx !!!!! :)
I tested it for tags and category description with WP 3.8.1
Pamela says
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
Victor says
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?
bj88 says
It does not work for me to enable shortcode on excerpt with the following code
add_filter( ‘the_excerpt’, ‘shortcode_unautop’);
add_filter( ‘the_excerpt’, ‘do_shortcode’);
Help :-)
manoj says
it worked thanks!
Holger says
Thanks so much ;) Works very well.
But do you like to help me out with a more heavy thing?
I want to have shortcodes in the WP Menu Custom Link URLs ;)
There is a Javascript Solution in an a little outdated plugin.. but it should work by injection some php too
Albert says
Any idea how to get this to work for an email? I tried doing it like this:
add_filter('user_registration_email', 'shortcode_unautop');
add_filter('user_registration_email', 'do_shortcode');
No luck.
Stepan says
How can I put my plugin’s widget in post&page content everywhere???
I put it in this page, but it shows in the top of the page, can I put it for example after text in this page??
please help me :-(
ashima says
Hye
This is my website http://lawnsalonllc.com/ as you can see i have paste two shortcodes in two custom sections of footer, but they are not working as these sections are residing in theme-editor.php i.e these are custom sections
i am very new to WordPress, can you please tell me how can i do that please?
Ashima
Franco says
Excellent, saved my day!
Brian Shim says
Great article. I used this technique to get Divi Builder content to show up in posts in archive pages.
Brian Shim says
One addition to this. I noticed that in archive pages, the tags were being stripped out using this technique. To get the tags back in, use this code:
$content = get_the_content();
$content = wpautop($content);
$content = do_shortcode($content);
echo $content;
Hope this helps,
Brian
Adam says
Under “Template Files”, shouldn’t that be
echo do_shortcode
? Adding the word “echo” before “do_shortcode”? Thanks for the article!Steven says
Thanks for sharing this article. Very helpful but how about titles?
Newy Web says
Hmm.. Trying to add this to a clients website on the footer, can’t seem to get it working. Will come back when I figure this out!
Eric says
Work like a charm: Thank you very much!
Kind regards from Germany!
Daniel says
hello. i have a question: is it possible to add shortcode support to my plugin via filter?
i want my plugin to listen to all shortcodes, just as a normale page/post would behave. so i have my plugin code and while this code is executed i want it also to listen to any shortcode call which will happen inside.
i hope you understand what i mean :) thanks!
Diana says
Works!
Thank you so much. Two days search – and your post made my day !!! ))
PotapProrn says
img
http://www.imagebam.com/image/8a7351800376853
http://imagestun.com/hosting/?v=26rzr.jpg
http://www.litetext.com/ldl7
http://fayloobmennik.cloud/7234207
Dennis Bareis says
I just tried adding the 2 lines to my OceanWP child theme (WP VERSION: 6.1.1).
It didn’t work for an image’s DESCRIPTION. It worked even without your 2 lines for a gallery image’s TITLE (advanced) of an image.
When it goes wrong WP mashes it and inserts unicode single quotes (x2019) when I use single quotes for the shortcode use and contents. Before trying single quotes it was inserting unicode double quotes.