Working With WordPress Shortcodes in Template Files

HOWTOs

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

Leave a Reply

Your email address will not be published.
*
*

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

Hints Web Development WordPress

Which WordPress Caching Plugin Is Right For You?

Most people can agree on one thing: they want their site to load faster. With a number of WordPress caching plugins now available – it is nice to see someone that did a comparison between the top contenders: http://www.tutorial9.net/web-tutorials/wordpress-caching-whats-the-best-caching-plugin/. Remember – your results will vary based on your hosting environment, your theme, your plugins, etc.

From Twitter

Twitter Weekly Updates for 2010-09-05

What a day! All kinds of events/drama! # @KYellowFarm are you in WI? in reply to KYellowFarm # Just beat latest Super Mario Bros with E this morning! Great game! #