Sometimes you might need to add a shortcode directly into a WordPress PHP template file to accomplish certain functions or display specific content across your site.
Why Direct Shortcodes Won’t Work
Adding a standard shortcode directly to a WordPress template file will not work out of the box because PHP files do not process WordPress shortcodes automatically like post or page editors do. Instead, we need to add the shortcode inside opening and closing PHP tags and invoke it with a special WordPress function.
How to Add the Shortcode
You can execute a shortcode within your theme’s PHP files by using the do_shortcode() function. Here is the correct syntax:
<?php echo do_shortcode('[name_of_shortcode]'); ?>
Simply replace [name_of_shortcode] with your actual shortcode, complete with any attributes you might need. Once you save the file, WordPress will correctly parse and render the shortcode’s output within the template.