Working With WordPress Shortcodes in Template Files

Ever need to include the functionality of a shortcode in a WordPress template file?

Awhile back I needed to create a template that auto changed some of the shortcode parameters depending on the URL it was accessed from. (Think single gallery page – that depending on the GET variables of the page – would show a specific and that gallery specific options. Instead of creating multiple gallery pages with the gallery shortcode hardcoded in – I decided to make one gallery page and then change the contents by simply modifying the URL using some GET variables which worked out great.

Example:
URL: http://www.example.com/gallery/?gallery-id=23

do_shortcode( '[jappler-gallery id=" '. intval( $_GET['gallery-id'] ).']' );

That code right there will then show the gallery with the ID 23 specified in the URL (?gallery-id=23).
This is just a quick way to utilize shortcodes inside the templates themselves while allowing you to make pages a bit more dynamic!

*Make sure to sanitize your GET variables.

Documentation on do_shortcode(): http://codex.wordpress.org/Function_Reference/do_shortcode

About the Author...

Jennifer Zelazny

My name is Jennifer Zelazny and this site is a collection of my random thoughts and opinions. I am Penn Stater, WordPress developer, and a modern day explorer. I love exploring data, trends, and things that make the world a better place and sharing them here.

Leave a Reply

Your email address will not be published.
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.