WordPress Wednesday: Turn Any Shortcode into a Template Tag
I previously discussed WordPress’s shortcode API, which allows you to turn any function into a shortcode that you can embed in the content of your pages or posts. Though the example I gave was for a function I wrote, you can use this API for WordPress template tags. The easiest way is to get the Template Tag Shortcodes plugin by Justin Tadlock, although you can also write your own by adding the following line to your functions.php file:
But what about the other way around? The solution is just as simple with the do_shortcode function.
You may remember that my previous shortcode allowed a client to enter [link pagename=’example’] into the content of any page, and the function would insert the named page’s permalink into the content for him. If I wanted to call that shortcode as a template tag, the tag would look like this:
<?php echo do_shortcode("[link pagename='example']"); ?>
All you have to do to turn any shortcode into a template tag is substitute your shortcode (with the desired parameters defined) between the double quotes (“) and you’re good to go. This is especially helpful when plugins come with shortcodes included but not template tags (like NextGen Gallery).
What will you use the do_shortcode function for?
Update: Thanks to Holling for pointing out that I forgot to include the echo statement. Fixed now.
- Better Includes and Excludes for wp_list_pages
- WordPress Wednesday: How to Use paginate_comments_links Only When You Really Mean It
- What Health Insurance Is and Isn’t
- WordPress Wednesday: Turn Any Shortcode into a Template Tag
- Survivor: Twitter – 3 Guidelines for Managing Who You Follow and Who Follows You
- Conversation with a friend today put me on this article. I realize this will be only mildly humorous to most of... fb.me/KK5hDtE3 6 months ago
- (SON OF) HALLOWEEN PLAYLIST Halloween Theme (John Carpenter, 1978) Another from the year I was born...a simple,... fb.me/876G0fUUF 6 months ago
- (SON OF) HALLOWEEN PLAYLIST The Batman Season 1 Intro (The Edge, 2004) It wasn't well-received, but I... fb.me/8vIl30r3e 6 months ago
- (SON OF) HALLOWEEN PLAYLIST Thriller (Michael Jackson, 1984) Created to "save the recording industry", this... fb.me/48i6qAMHO 6 months ago
- Zeb fb.me/7gJeH19qo 6 months ago
- An error has occurred; the feed is probably down. Try again later.