WordPress : Run shortcode in PHP template file
Join the DZone community and get the full member experience.Join For Free
WordPress shortcode is very useful while creating new posts or pages. It is a nifty way to perform certain tasks in the post. For instance, if you want to add a youtube video or image gallery then WordPress shortcodes are quite useful.
Many plugins provide their own shortcodes for displaying certain content or performing some tasks. However, you might require the same plugin functionality in a theme file. Since the shortcode tag is not a standard one so you cannot write it in the PHP file. WordPress provides a wrapper function to make this shortcode work in the PHP file.
Just paste the following PHP code at the place where you want that shortcode to work.
<?php // Use shortcode in a PHP file (outside the post editor). do_shortcode('[your_own_shotcode]'); ?>
It’s an awesome way to use a shortcode inside the template files. Hope it helps.
Opinions expressed by DZone contributors are their own.
The SPACE Framework for Developer Productivity
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
Comparing Cloud Hosting vs. Self Hosting
Competing Consumers With Spring Boot and Hazelcast