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:
- Open your theme header.php file the file that displays the header
- 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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Redirect On Refresh WordPress Page » Detect Page Refresh PHP
- How to Create WooCommerce Login Logout Shortcode
- Top 30+ Best WordPress Form Plugins » Best WordPress Form Plugin
- How to Check if a User is Logged In to WordPress
- How to Get Order Details After Checkout In WooCommerce
- How to Create Checkbox Field in WooCommerce Checkout Page
- How to Create My Account Page In WooCommerce
- How to Set up WooCommerce Storefront Footer Widgets
- How to Add Pagination in WordPress Post Split into Multiple Pages
- Remove Hash # from WordPress URLs with Simple Trick
- How to Clear Cart on Logout In WooCommerce
- How to Setup WooCommerce Storefront Blog
- How to Use Shortcode in WordPress PHP Theme File Templates
- 35+ Best Free Form Plugin WordPress
- How to Use WordPress Shortcodes in Pages or Posts with Example
- How to Change WooCommerce Checkout Page Title
- How to Refresh Checkout Page WooCommerce
- First 20 Steps for WordPress Complete Beginners to Get Started
- How to Move WooCommerce Checkout Field