One of the best things (OK, the best thing) about development with WordPress is piggybacking on the available plugins. If you are doing custom development chances are that a plugin probably wasn’t made with your specific use case in mind, so it’s often necessary to some work to get the plugin’s functionality and your goal to match. With that in mind, you have probably recently asked yourself, “Can I use a WordPress shortcode in my theme, as opposed to just inserting it into the text body?”
Good news,: you definitely can.
<?php echo do_shortcode("[shortcode]"); ?>
That’s it — all the code you need to use shortcake in your template or theme. Super easy right? You can easily augment this with your PHP variables or Advanced Custom Fields with something like:
<?php echo do_shortcode("[shortcode=" . yourVar . "]"); ?>
This all relies on the WordPress function “do_shortcode()” which is handy but underappreciated. While developing you might feel the need to rebuilt the function of your shortcode into your template, but this is a much simpler, more expedient, and WordPressy way to do it.
And this is useful for the most obvious reason, and the reason you are here: shortcodes are generally designed for use by a writer or editor to insert a function into an article, but here you can do that at the template or page-type level, which means you can do all your pages at once, not edit them one by one.
Sal Cangeloso May 3rd, 2019
Posted In: Web Development