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.