WordPress – Add Shortcode In a PHP Template Page

This post shows you how to add a Shortcode in a WordPress PHP template page instead of adding it inside the content of a Post or Page.

 

1. WordPress’s do_shortcode() function

In order to do this, we need to use do_shortcode function:

do_shortcode( string $content, bool $ignore_html = false )

 

  • $content (required): where you need to pass the shortcode name.
  • $ignore_home (optional): By default it is false. If you set it to true, the shortcodes inside HTML elements will be skipped.

 

2. Usage

your-template.php
<?php echo do_shortcode('[shortcode]'); ?>

 

3. Example

The below is an example showing how to add a Contact Form 7 shortcode in our WordPress PHP templage page.

my-template.php
<?php echo do_shortcode('[contact-form-7 id="100" title="Contact Form"]'); ?>

Leave a Reply

avatar