Use WordPress Shortcodes outside the Editor

The Shortcode-API can help to make your Editor more advance especially with the help of Shortcodes to insert external content to your posts or pages easily. There are many possibilities, as you can see in this example Easy Way For Advertisement.

But if sometimes you like to use a Shortcode in your Template or just don’t like to put it in the Editor, for example you like to insert it in the Custom Field or somewhere else. There is a function in WordPress which helps you to insert a shortcode somewhere else besides your Editor.

The easiest method is to access a shortcode, for example myshortcode; with the help of do_shortcode(). I will show you a code example; you put it into your Template – for example in the single.php of your Themes.

echo do_shortcode('[myshortcode]');

If you work with parameters, then the Shortcode can process these prarameters and they can be delivered.

echo do_shortcode('[myshortcode param="blahblahblah"]');

Please Note: PHP differentiate ‘ and “, this can become a problem for a beginner. For example the following code wouldn’t work:

echo do_shortcode('[myshortcode param='blahblahblah']');

this works

echo do_shortcode('[myshortcode param="blahblahblah"]');

Another example is to use a value from the Custom Field example-name and deliver it to the Shortcode example_shortcode for the parameter product.

$example_product = get_post_meta( $post->ID, 'example-name', $single = true );
if ($example_product)
	echo do_shortcode('[example_shortcode product="' . $example_product . '"]');

Therefore you have many possibilities, for example access to the gallery, which you maintain in a post.

echo do_shortcode('[gallery option1="value1"]');

With this you can easily load a gallery and is not depending on Shortcodes in your Editor. Also you save the parsing of the filter.

Comments are closed.


  1. ozh

    Maybe for some clarity, the function is not do_shortcode( “[myshortcode]”), but more broadly do_shortcode( $some_text ) where $some_text can contain any number of shortcodes.

    Just not to let people think that do_shortcode is tied to one particular shortcode.

  2. Cosmin

    @ozh – thanks for adding that 🙂

    Guys, any idea on how to run shortcodes in custom widgets created for the WP admin dashboard?


  3. olivier

    @Cosmin : You can run shortcodes in widget. Simply install an plugin like “PHP Code Widget” that allow you to insert php code in a widget.

  4. matt mcinvale

    used this technique while building geoposty ( to allow shortcodes in widgets. works great. 🙂

  5. Jason

    I currently use echo do_shortcode(“[gallery link='file']“) which works, but it has the ‘ and ” switched. Is there any significant difference?

    I am using this technique quite a lot, i am just wondering if this adding any overhead or if the order is interchangeable and matter of preference.

  6. Harley

    Thank you so much for this article! I have been looking around for a way to insert shortcodes into php functions for a little while now and I came across your fantastic article which is exactly what I needed. I use several custom shortcodes which I’ve created for each type of mp3 player I use on my blog . If you check it out you will see that each mp3 is contained in its own “block” which includes the artist, title, genre, audio player, download link, and share on facebook link. That is all done using shortcodes! Send me an email if you’d like to see my code.


  7. Steve

    Not sure if this is the best place to ask, I want to create a shortcode that generates a button -that’s the easy part.

    When the button is clicked (on a WP Page) is it possible to then execute another shortcode? Basically I want to insert some rows into a custom table and then redirect the user to a specified WP page after it completes.

    I was thinking of using wp-load.php within a custom php file – not sure if there is a better way to approach it though?

  8. Jim C

    very simple, but thanks for posting. i’m sure i will use this in the not too distant future..

    more often we want to create widgetised page templates outside of the sidebar, and with this we can improve our productivity when doing this..

  9. Govpatel


    I have been looking for way to display testimonials in rows on my website.
    The Tb Testimonial plugin gives a short code and instructions says that I use single.php code as testimonials.php .

    I need to know where I need to add the code you have above in template so that the testimonials are displayed in rows,


  10. Govpatel

    Correction on my post I meant Columns not rows.

  11. Alberto

    I think you might have just saved my life with this post… will be testing tonight. Thanks for writing.

3 pingbacks

  1. Usando Shortcodes en nuestro theme | aNieto2K
  2. Community News: Plugin process edition | WPCandy
  3. Como usar shortcodes do Wordpress fora do Editor