Do you wish to use WordPress shortcodes in your theme?
Shortcodes are typically utilized within content areas like as posts, pages, and sidebar widgets. However, you may want to use a shortcode within your WordPress theme on occasion.
We’ll teach you how to simply add any shortcodes to your WordPress theme in this article.
What is a WordPress Shortcode?
In WordPress, a shortcode is a specific tag that allows you to insert dynamic features into content sections such as posts, pages, widgets, and more.It’s a text string enclosed in square brackets. This is how they usually appear:
What exactly do we mean when we say “dynamic elements”? A shortcode can be used to add image galleries, forms, social media feeds, and many other features to your WordPress site.
Many popular WordPress plugins employ shortcodes, and WordPress comes with a few built-in shortcodes.
WPForms, for example, features simple blocks as well as shortcodes that you can use to place forms wherever on your site.
See our entire guide on how to add a shortcode in WordPress for more information on shortcodes.
Why Use Shortcodes in Your WordPress Theme and When Should You Use Them?
Shortcodes are typically utilized within the content regions of a WordPress website. You may easily add them when editing your content in the WordPress admin section.
In WordPress, for example, you can use the block editor to add shortcode.
Some expert users, on the other hand, may prefer to use a shortcode within their WordPress theme files. If you use the same format for the shortcode as you use for content areas, it will not work.
You’ll see the shortcode itself printed on the screen rather than the output of your shortcode.
Because WordPress does not execute shortcodes within theme template files, this is the case. You must specifically instruct WordPress to run the shortcode by using the do shortcode function.
Here’s how to use it in your WordPress theme files with some sample code.
1
|
|
Remember to change ‘gallery’ with the shortcode you’d like to use.
Additionally, if you wanted to add a shortcode with other parameters, the syntax of this code would alter slightly.
The following is an example of a WPForms shortcode that includes the form ID as an additional parameter.
1
|
echo do_shortcode( "[wpforms id='147']" ); |
What should you do if your WordPress do shortcode isn’t working?
The majority of shortcodes rely on WordPress plugins or additional code on your website.
If your website’s do shortcode function isn’t working, make sure the plugin that provides the shortcode is installed and activated.
Similarly, if the shortcode is given by your WordPress theme or any custom code snippet, double-check that it is functional.
The shortcode exists() function is a simple approach to troubleshoot and fix these issues. This function just checks if a shortcode is available.
Here’s an example of how to utilize the do shortcode() method with it.
1
2
3
|
if ( shortcode_exists( 'wpforms' )) { echo do_shortcode( "[wpforms id='147']" ); } |
If you’re still having trouble seeing the shortcode output on your website, consider clearing your WordPress cache to ensure you’re not looking at an outdated version.