Discover the steps to use WordPress do_shortcode.

What is a shortcode?

Shortcodes are snippets of codes that allow you to run a function such as displaying content. Let's understand this with the help of an example. If you create a slider with the help of the MetaSlider plugin, then you need to embed the plugin's shortcode into your site to show the slider. But, you are only allowed to embed it on your site's body or widgets, or else the content won't get displayed. To resolve this issue you can use the WordPress do_shortcode function. By using this function you can embed the shortcode anywhere you want (in the header also). The code will look like below:

<?php echo do_shortcode( '[your shortcode goes here]' ); ?>


In order to apply this function, you need to go to the theme files and tweak them. Make sure that you have a backup of your full site and use the WordPress child theme to avoid data loss.

How to use do shortcodes?

Now let's add a shortcode using WordPress do_shortcode. In this example, we will add a slider in the theme's header using MetaSlider (If you do not have the MetaSlider plugin then install it). However, there is no widget for the header area of your site.

That's why you need to embed the shortcode of the slider inside the theme's header file. To achieve this, follow the steps given below:

1. In the WordPress admin dashboard click on the MetaSlider option present on the left sidebar.

2. Then copy the shortcode present at the bottom right corner of the page.



Let's say the MetaSlider’s shortcode is already present within the WordPress do_shortcode function. Then you have to edit the theme’s header file by going to your hosting provider’s file manager or an FTP client like FileZilla.

The below steps will show you how to edit theme files with files manager using cPanel. You can also use an FTP client for the same purpose.

  1. First, log into cPanel and click on File Manager.                                            
  2. Then, go to the public_html folder and move to wp-contents -> themes.                                                                                                  
  3. Navigate to your currently active theme folder.                                                     
  4. Open header.php file. Then scroll down the page and add the do_shortcode function above the  </header> tag.                                         
  5. Click on the Save button and check your website's appearance.

Sometimes when you edit your theme core files it can break your WordPress template. To solve this you should alter your CSS to make the appearance of your website better. 

  That's it. From this article, you have learned how to use shortcodes and display them using the WordPress do_shortcode function. Make sure that you have a backup copy of your website before beginning the process, as you can lose your data during the process.

Did you find this article useful?