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:

<br />
add_filter( 'widget_text', 'shortcode_unautop');<br />
add_filter( 'widget_text', 'do_shortcode');<br />

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:

<br />
<?php do_shortcode('[foo]'); ?><br />

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:

<br />
<?php<br />
$content = get_the_content();<br />
echo do_shortcode($content);<br />
?><br />

Comments

Do you trust your commenters enough to allow them to use shortcodes? This goes into functions.php:

<br />
add_filter( 'comment_text', 'shortcode_unautop');<br />
add_filter( 'comment_text', 'do_shortcode' );<br />

Excerpts

To enable shortcodes in excerpts, add these lines to your functions.php file:

<br />
add_filter( 'the_excerpt', 'shortcode_unautop');<br />
add_filter( 'the_excerpt', 'do_shortcode');<br />

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:

<br />
<?php<br />
// $user_id = 3;<br />
$userdata = get_userdata($user_id);<br />
echo do_shortcode($userdata->description);<br />
?><br />

Category, Tag, and Taxonomy Descriptions

These descriptions can be filtered, so the code for your functions.php file is simple:

<br />
add_filter( 'term_description', 'shortcode_unautop');<br />
add_filter( 'term_description', 'do_shortcode' );<br />

Posted on February 13, 2010 at 10:48 am in WordPress · 15 comments

Sharing this post? The short URL is http://sillybean.net/?p=2719

15 Responses to “using shortcodes everywhere”

  1. norrige dalla says:

    Does not work on 2.9.2 at least.

  2. Stephanie says:

    I just tested again in 2.9.2 and it’s working beautifully. Which one did not work for you?

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

  4. 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!

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

  6. 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?

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

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

  9. Ryan says:

    sorry forget to check notify!

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

    <?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'); ?>

Leave a Reply