How to Insert Shortcode in PHP Template

Wordpress Shortcodes

Many plugins provide shortcode which you can directly add into the content anywhere you like. But most of these plugins dont provide clear information on how to add them to our template so that it automatically shows in a particular location everytime a new page or post is created. Here are the steps to add a shortcode into your template.

  1. Click on Appearance on the left side bar inside your WordPress Admin backend
  2. Click on Edit
  3. Choose the particular page file or post file you wish to edit. Most of the default templates have page file and post file as page.php and post.php. If it should be on your sidebar or footer, choose that particular file which contains the code for your sidebar or footer.
  4. Clicking on the file on the list to your right will open the editor for you
  5. Choose the location where you wish the shortcode to appear.
  6. If your shortcode is [pluginshortname gal=1] then envelop it with echo do_shortcode(”); so the short code looks like echo do_shortcode(‘[pluginshortname gal=1]’);
  7. If the code would be inside a PHP code then the above line alone should be enough. If its inside a HTML code, then it would look like the below:
    <?php
    echo do_shortcode(‘[pluginshortname gal=1]’);
    ?>
  8. Please ensure that the single quotes are proper. Sometimes when you copy, a wrong single quote might be added which can break your website and show an error in the front end.