Home > Web Design, WordPress > WordPress Wednesday: Turn Any Shortcode into a Template Tag

WordPress Wednesday: Turn Any Shortcode into a Template Tag

March 3, 2010

I previously discussed WordPress’s shortcode API, which allows you to turn any function into a shortcode that you can embed in the content of your pages or posts. Though the example I gave was for a function I wrote, you can use this API for WordPress template tags. The easiest way is to get the Template Tag Shortcodes plugin by Justin Tadlock, although you can also write your own by adding the following line to your functions.php file:

add_shortcode('shortcode_name', 'template_tag');

But what about the other way around? The solution is just as simple with the do_shortcode function.

You may remember that my previous shortcode allowed a client to enter [link pagename=’example’] into the content of any page, and the function would insert the named page’s permalink into the content for him. If I wanted to call that shortcode as a template tag, the tag would look like this:

<?php echo do_shortcode("[link pagename='example']"); ?>

All you have to do to turn any shortcode into a template tag is substitute your shortcode (with the desired parameters defined) between the double quotes (“) and you’re good to go. This is especially helpful when plugins come with shortcodes included but not template tags (like NextGen Gallery).

What will you use the do_shortcode function for?

Update: Thanks to Holling for pointing out that I forgot to include the echo statement. Fixed now.

  1. March 17, 2010 at 8:56 AM

    Thanks for the tip! I was looking all over for it!

  2. david swain
    March 22, 2010 at 3:13 AM

    I cant get it to work on the embed shortcode, any ideas?

  3. April 5, 2010 at 2:46 PM

    It appears you need to use do_shortcode inside an ‘echo’ statement. See this link for an example:

    cheers – H

  4. April 5, 2010 at 4:36 PM

    @Yahzee – no problem — I posted it because it took me forever to find!

    @david – Sorry I didn’t reply right away. I’ve been pretty buried and haven’t been able to get back to the blog. Holling is totally right, though.

    @Holling – Thanks for stepping in there. Funny thing is, I have it written right where I’ve used it in templates, but somehow dropped the echo when posting it here. I’m updating the post to fix the error.

  5. April 27, 2010 at 4:24 AM

    Clear and easy to understand instructions to embed a plugin shortcode into a template file. Thanks!

  6. July 4, 2010 at 6:16 AM

    Excellent tutorial, This is exactly what I needed.

  7. November 1, 2010 at 8:45 PM

    Hey nice tutorial
    but i have a problem
    i have a wordpress plugin Taxonomy Terms List, this plugin is used to display all the custom taxonomies exactly next to post,
    i want to change the position of it
    i just want it to make above the post
    just like this, check this image

    the plugin is also too short

    if( !function_exists( ‘pr’ ) ) {
    function pr( $var ) {
    print ” . print_r( $var, true ) . ”;

    if( !function_exists( ‘mfields_taxonomy_terms_list’ ) ) {
    add_filter( ‘the_content’, ‘mfields_taxonomy_terms_list’ );
    function mfields_taxonomy_terms_list( $c ) {
    global $post;
    $o = ”;
    $terms = array();
    $lists = array();
    $custom_taxonomy_names = array();
    $custom_taxonomies = mfields_get_custom_taxonomies();
    if( !empty( $custom_taxonomies ) )
    foreach( $custom_taxonomies as $name => $config )
    $custom_taxonomy_names[] = $config->name;
    if( !empty( $custom_taxonomy_names ) )
    $terms = get_terms( $custom_taxonomy_names );
    foreach( $custom_taxonomies as $name => $config )
    $o.= get_the_term_list( $post->ID, $name, $before = ” . $config->label . ‘: ‘, $sep = ‘, ‘, $after = ” );
    if( is_single() )
    return $c . $o;
    return $c;

    if( !function_exists( ‘mfields_get_custom_taxonomies’ ) ) {
    function mfields_get_custom_taxonomies( ) {
    global $wp_taxonomies;
    $custom_taxonomies = array();
    $default_taxonomies = array( ‘post_tag’, ‘category’, ‘link_category’ );
    foreach( $wp_taxonomies as $slug => $config )
    if( !in_array( $slug, $default_taxonomies ) )
    $custom_taxonomies[$slug] = $config;
    return $custom_taxonomies;


    can you help me out in this????

  8. December 14, 2010 at 11:48 AM

    This has really helped me with a new template! Many thanks.

  9. December 23, 2010 at 9:59 PM

    Hi, interesting tips for developer. Although i found a plug in which will let you embed an internal link to post or page or category or author in you site with just a simple shortcode. Check it on wordpress http://wordpress.org/extend/plugins/w4-internal-link-shortcode/ .

  10. Chris Boggs
    January 17, 2011 at 9:43 AM

    Many thanks for this tutorial Michael. This worked in my custom index.php file right away with your simple and clear instructions. Thanks again, -c-

  11. Alberto
    January 26, 2011 at 6:13 AM

    Thanks for the tutorial – very helpful. I have a question though, is there anyway to insert php code inside the shortcode.

    For example, using what you wrote:

    Would that be possible?

  12. Alberto
    January 26, 2011 at 6:13 AM

    Thanks for the tutorial – very helpful. I have a question though, is there anyway to insert php code inside the shortcode.

    For example, using what you wrote:

    Would that be possible?

    Thank you!

    • Alberto
      January 26, 2011 at 6:15 AM

      Sorry the code did not paste above. Using your example in the post, is there anyway to replace ‘example’ with php code. Like this:

      [link pagename=’example’] ===> [link pagename=’SOME PHP CODE’]

  13. daviddj
    June 29, 2011 at 12:55 PM

    very god

  14. Stephen Siu
    November 20, 2011 at 9:21 PM

    wow amazing, this did the trick for embedding a contact form into the sidebar of my theme.

  1. April 29, 2010 at 9:19 PM
  2. March 14, 2011 at 2:10 PM
Comments are closed.
%d bloggers like this: