Home > Tutorials > Wordpress Tutorials > Add Any Shortcode To A WordPress Page Template

Add Any Shortcode To A WordPress Page Template

Wordpress offers thousands of different plugins that let you add alot of different functions and features to your website. Furthermore most of these can easily be integrated across your website with a few simple short-codes which you simply copy paste into the WordPress editor. However what do you do when you want to integrate that shortcode into your custom template pages? Or maybe you want to add something to your page.php file so that the new feature/function is automatically added to every new page?

This is where the do_shortcode function comes into play. With do_shortcode you can easily use any plugin’s shortcode within your WordPress theme files. So lets say we have a gallery plugin which has a [show_gallery] shortcode. If we want to add this within our page template file we simply use the following syntax:

echo do_shortcode('[show_gallery]');

The same concept applies to the longer shortcodes along with their own variables, as provided by Contact Form 7:

echo do_shortcode('[contact-form-7 404 "Not Found"]');

You can also take things a step further and store the shortcode inside of a variable. This allows you to then call upon the variable whenever you wish to use the shortcode. So let’s say we are creating a custom page which will display either an Order or a Quote form. We can store the necessary shortcodes for our forms into two different variables, and then make us of the PHP if/else functionality to choose which form to display.

$quoteForm = do_shortcode( '[contact-form-7 404 "Not Found"]' );
$orderForm = do_shortcode( '[contact-form-7 404 "Not Found"]' );

if ($formType == "Quote") {
	echo $quoteForm;
} else {
	echo $orderForm;

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

Looking to Fortify Your WordPress Website’s Security? Try MalCare Security Plugin!

30% of sites on the internet are being powered by WordPress today – making it ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.