If you have used WordPress for a while, you must have realized that using the do shortcode function can be a good way to add more functionality to your pages and posts. However, most WordPress users aren’t aware that you can add Shortcode to any PHP Page template you have on your website, as long as your site runs on WordPress.
Note: You must the appropriate permissions to edit or modify the WordPress Template file in question, for you to call shortcode in PHP code successfully.
Let’s continue J. The fact that you can even do shortcodes from Widgets embeds, captions and galleries, hypothetically tell you that you can always move or add any part of your website to any desirable location: That is, if you use this simple WordPress shortcode PHP tweak that we are about to show you.
This article is suited for any person who wishes to insert shortcodes in PHP code, specifically if they want to add a shortcode to a WordPress template that does not have a widget area.
Moreover, you might be working with a WordPress template that has a widget area, but it isn’t correctly positioned in the area that you want to add your shortcode. For this scenario too, you will definitely have to tweak your code in that specific area so that PHP can echo and do shortcode correctly (in your desired location).
Adding do shortcode to PHP code
Calling any Shortcode in HTML or PHP requires you to write the call as a function. If you try to paste the shortcode directly into the WordPress PHP template, it won’t work and probably it could render that page unusable. The WordPress function that allows users to integrate any shortcode in PHP is called the “do_shortcode” function.
There are two ways to – do_shortcode in a template – and any of these two options will work in case the do_shortcode function is not working in any of your page templates. In our case, the first one worked but in another forum, we found that the second function worked just fine with some users. Try both and choose the one that works best for you.
Add any of these two PHP codes within the page’s template so as to show any shortcode within the WordPress page in the front-end:
<?php echo do_shortcode(‘[Your_shortcode]’) ?>
<?php echo do_shortcode(“[Your_shortcode]”); ?>
Note: Replace Your_shortcode with your desired shortcode as shown in the “do_shortcode example” below. There is not much difference in the functions above apart from the fact that one uses single quotes (‘) and the other one uses double quotes (“) – But it could make the difference if one fails.
Check out these do_shortcode examples
To show Woocommerce featured products in a WordPress page template paste this within the Page’s code :
<?php echo do_shortcode(‘[featured_shortcode]’); ?>
<?php echo do_shortcode(“[featured_shortcode]”); ?>
Watch Video Below to get an idea: