How to use do_shortcode in WordPress (PHP)

php do_shortcode
WordPress Shortcode gives users additional functionality to add unique content in pages, posts, and, widgets sidebars. In the past, I explained how to use shortcodes on a page or post.

Several plugins and themes come with shortcodes and allow you to dynamically add content to pages or posts and even templates.

To make the shortcode work in templates you need to make use of the PHP do_shortcode function in WordPress.

WordPress PHP do_shortcode

Shortcodes in WordPress are text you can use in the posts, pages, and templates to add some function to add new features that do not exist in the default WordPress settings.

For example, most popular WordPress plugins like the WooCommerce plugin use shortcodes to publish pages such as the cart page, and checkout page.

You can create your own shortcodes, and plugins often allow their functionality to be extended via shortcodes.

If you want to use a shortcode from in a PHP template as opposed to adding it on the post or page, how can you accomplish this?

You can use a WordPress core function do_shortcode() that allows you to publish the shortcode on templates inside PHP tags.

The following is a general example of how the do_shortcode() code works :

<?php echo do_shortcode("[shortcode]"); ?>

How To Use WordPress do_shortcode

Using this do_shortcodefunction to extend site functionality is easy since you just need to know where to place the code in the theme files and then just add the code and replace the shortcode example above with your shortcode.

You can place this function on any page template – single, archives, home, and page of your theme.

Now, let us use the do_shortcode function to add a shortcode example – [njengah-shortcode-example]   For instance, we assume you want to include a widget in your theme’s header using the custom shortcode – [njengah-shortcode-example]  – first, ensure you have installed the plugin or have the code that creates this shortcode in the functions.php.

So, to add the shortcode of the widget to the theme’s header file, these are the steps you should follow:

  1. Open your theme header.php file the file that displays the header
  2. Copy the shortcode below and save the changes to display the custom functionality in your theme header.
<?php echo do_shortcode("[njengah-shortcode-example]"); ?>

Sometimes, when you are editing theme core files, you need to be careful to avoid breaking your WordPress site. To resolve it this issue you need to put your backup code back and go back to the original state of your site before you implemented the changes.

Conclusion

In this post, you have learned how to use PHP do_shortcode to display shortcodes in the templates. You just need to swap my example shortcode with your shortcode and it should work.

If you are not sure where to place it, please write to me I will help you quickly place this shortcode on your template.

Get in touch with me using the form on this contact page. For advanced shortcode creation, customization, and complex tasks you may want to hire a WordPress expert.

Similar Articles