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.
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!