WordPress code

Execute Shortcodes in your Theme Files

| 2 Comments

Shortcodes are very useful for quickly tapping into predefined functionality or appearances, set by either plugins or themes. The trouble is that if you include them in a theme file as raw code, they’ll just appear as plain text and won’t be executed as you might hope or expect. So in order to execute a shortcode in a theme file, to get a particular style for something, or to execute a function, you need to do the following.

Using shortcodes in theme files

When you go to edit the theme file where you want to use the shortcode, all you have to do is insert the shortcode content that you want to execute inside a filter, which will execute the shortcode that you put inside.

For example, say a theme comes with shortcodes for creating columns of one third and two thirds, that you want to hard-code into a particular part of your theme. So your shortcode might look something like this:

Now, if I want to use this within a theme file, I can enclose it within a WordPress function – do_shortcode. So in my case, we want to echo the results of performing a do_shortcode on the content I wrote out above. So the line of code would look like this:

Once you insert that, save it and refresh the page, you should see that the shortcode has been executed and you see the content as you intended it.

Categories: Code & Snippets, Themes | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler

Author:

Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

2 Comments

  1. Hi Dave,

    Thanks for the post.

    I have one question, please excuse my lack of knowledge.
    As the Editor no longer appears to be present on WP, in order to add this code do we access a particular file in the back end?

    Many thanks, really enjoying your site.

    Richard C

    • The editor is still available, assuming you have a self-hosted site and not a wordpress.com site, and your access to it has not been restricted. You can edit it directly from your host’s file manager though instead of through the built-in file editor in WordPress.

Leave a Reply