You can easily extend your siteโs functionality by using shortcodes. Shortcodes can be placed on any part of your website by using WordPress do_shortcode function. In this article, we will show you how to apply it. So, letโs start by understanding the basics of shortcodes!
What Exactly is Shortcode?
A snippet of code, shortcode allows you to run a function such as displaying content. For instance, if you create a slider with the help of the MetaSlider plugin, you need to embed the pluginโs shortcode into your site to show the slider.
But, you can only embed it on the siteโs body or widget or else, the content wonโt get displayed. For resolving this issue, you can use WordPress do_shortcode function. By using it you can embed the shortcode anywhere you want โ including the header. It looks as below:
<?php echo do_shortcode( '[your shortcode goes here]' ); ?>
To apply this function, go to the theme files and tweak them. Ensure that you take a complete backup of your WordPress site and use the WordPress child theme in advance to avoid data loss if anything goes wrong.
Steps to Use WordPress do_shortcode
Now, letโs try to add a shortcode using WordPress do_shortcode. For instance, suppose you want to add a slider in your themeโs header using MetaSlider โ ensure youโve installed the plugin. However, there is no widget for the header area.
Therefore, you need to embed the shortcode of the slider to the themeโs header file. For this, follow the below steps:
- Click onย MetaSliderย located on the left sidebar of the WordPress admin dashboard.
- Go to the bottom right of the page and copy theย shortcode.
If the MetaSliderโs shortcode is already present within the WordPress do_shortcode function then 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 the steps to edit theme files with MilesWebโs file manager โ it can be applied in an FTP client as well:
- Hover to your cPanel and click onย File Manager.
- Navigate to theย public_htmlย folder and move toย wp-contents -> themes.
- Find and go to your currently active theme folder.
- Openย header.php, then scroll down the page and add theย do_shortcodeย function above the tag.
- Save and check your website appearance.
Sometimes when you edit theme core files, it can break your WordPress template. For this, you should alter the CSS to improve your websiteโs appearance.
Conclusion
In this way, you have learned to use shortcodes and display them using the WordPress do_shortcode function. Just add the shortcode into the themeโs file, and youโre ready to go! Remember to take the backup of your site before editing the file to avoid the risk of losing data.
If you have any questions regarding the topic, drop a comment below!


