Usually, the shortcodes are used within content elements such as posts, pages, or sidebar widgets. Yet, there are occasions when you may wish to add a shortcode directly within the framework of your WordPress theme.
Many WordPress plugins and themes rely on shortcodes to help you seamlessly add important features to your website. Therefore, learning how to use shortcode proficiently in WordPress becomes imperative.
Without this knowledge, you might miss out on a key part of the WordPress experience. Fortunately, using shortcode in WordPress is easy, even users with minimum technical expertise can use it.
Before we start the process of adding WordPress shortcodes, it is essential to understand why we use shortcodes in your WordPress themes.
Why You Need to Use WordPress Shortcodes in Your Website Theme?
Utilizing shortcodes in your WordPress themes offers a number of benefits that contribute to the overall functionality and user experience of your website.
These handy snippets empower users to effortlessly incorporate various elements such as buttons, forms, and galleries to provide a user-friendly experience. By leveraging both built-in and plugin-provided shortcodes, you can improve your website’s functionality and visual appeal with ease.
At times, incorporating a shortcode directly into your WordPress theme files becomes essential. This helps in integrating dynamic elements into areas that aren’t editable through the standard WordPress post editor, such as your 404 page. Moreover, it offers a convenient means of implementing the same shortcode across multiple pages.
How to Use Shortcodes in Your WordPress Theme?
Implementing WordPress shortcodes is an easy process. There are different methods to do so. No matter, if you choose to directly add them to posts or pages, utilize a shortcode plugin, or explore other methods, implementing WordPress shortcodes offers flexibility in boosting your website’s functionality.
1. Utilizing Full-site Editor (For Block-Enabled Themes)
The most straightforward way to incorporate shortcodes into your WordPress theme is through the full site editor.
This is an exclusive method specially designed for block-based themes like Hestia Pro, Astra, Kadence, and many others. However, for themes without block capabilities, an alternative approach is necessary.
To get started go to Themes and click on Editor in the WordPress dashboard.
The default view of the full-site editor displays your theme’s home template, but shortcodes can be added to any template or template part. You can select the header or footer in the ‘Templates’ to add shortcodes.
Once you’ve chosen the desired template or template part, proceed by clicking on it. For illustration, let’s consider adding a shortcode to the 404-page template. These steps remain the same for any selected template.
WordPress will present a preview of the chosen template or template part. Now it’s time to insert a shortcode. Click on the small pencil icon and then on the blue ‘+’ icon in the top left corner.
In the search bar, type ‘Shortcode’ and drag the corresponding block onto the theme template. You can now input or paste the desired shortcode. Now click on the ‘Save’ button to save your changes.
Upon completion, visit your WordPress blog to witness the shortcode in action, as represented on a 404-page template.
2. Using WordPress Theme Files
This method works with any WordPress theme. To incorporate shortcodes into your WordPress theme, an advanced method is used.
Users can make direct changes to individual theme files. However, it is not recommended as it can complicate the process of updating your WordPress theme without losing your customizations. Therefore, we suggest creating a child theme to override the theme files instead.
If you’re working on a custom theme, direct modification of existing theme files is an option. However, be aware that inserting shortcodes in the same manner as standard content areas won’t give the expected output.
Unlike in standard content areas, WordPress doesn’t automatically execute shortcodes within theme template files. Instead, you must explicitly instruct WordPress to run the shortcode using the do_shortcode function.
Here’s an example of the code snippet to add to your WordPress theme files:
echo do_shortcode(‘[slider]’);
Replace ‘slider’ with the specific shortcode you intend to use. When incorporating a shortcode with additional parameters, such as in the case of a WPForms contact form, adjust the code snippet accordingly:
echo do_shortcode(“”);
In cases where the do_shortcode function doesn’t produce the expected output, it may indicate a dependency on a WordPress plugin or other code.
Confirm that the plugin providing the shortcode is installed and activated in the Plugins » Installed Plugins section.
Now here, you can also use the shortcode_exists() function to verify the availability of a shortcode before implementing it in your theme files.
If the shortcode output still doesn’t appear, you might have to consider clearing the WordPress cache. It’ll allow you to view the most up-to-date version of your site.
“do_Shortcode” is Not Working. How to Troubleshoot It?
If the do_shortcode function isn’t functioning as expected, make sure that the plugin providing the shortcode is both installed and activated. To do so go to Plugins » Installed Plugins.
Furthermore, you can also check the availability of a shortcode for use by adding the shortcode_exists() function to your index.php file.
In the given code snippet, we are verifying whether the WPForms snippet is accessible for use on our website:
if ( shortcode_exists( ‘wpforms’ )) {
echo do_shortcode(““);
}
If the shortcode output is still not visible on your website, consider clearing the WordPress cache, as you might be viewing an outdated version of your site.