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.


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

echo do_shortcode('');

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.


Posted

in

by

Comments

14 responses to “Use WordPress Shortcodes outside the Editor”

  1. […] vimos como funcionaban los shortcodes y como implementarlos en nuestros WordPress. Ahora en WPEngineer publican una interesante artículo en el que nos explican como usar shortcodes en nuestros themes sin necesidad de encontrarnos dentro […]

  2. ozh Avatar

    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.

  3. Cosmin Avatar

    @ozh – thanks for adding that 🙂

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

    Thanks!

  4. olivier Avatar

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

  5. matt mcinvale Avatar

    used this technique while building geoposty (http://geoposty.com/) to allow shortcodes in widgets. works great. 🙂

  6. […] Frank from WPEngineer on how to use WordPress shortcodes outside of the WYSIWYG editor. […]

  7. […] e curiosos que realizam modificações a templates wordpress. Recentemente o pessoal da wpengineer falaram sobre o assunto, e aproveitámos o esclarecimento para lhe trazer nova informação. A […]

  8. Jason Avatar

    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.

  9. Harley Avatar

    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 http://kingcrunk.us . 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.

    Thanks!

  10. Steve Avatar

    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?

  11. Jim C Avatar

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

  12. Govpatel Avatar

    Hello

    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,

    Govpatel

  13. Govpatel Avatar

    Correction on my post I meant Columns not rows.

  14. Alberto Avatar

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