WordPress Wednesdays: Use shortcodes in theme files

Geek Factor: 3

Filed under ‘something I wish I knew sooner’: this week’s WordPress Wednesday covers a simple little built-in function that can save you a lot of mucking around in WordPress:

In short (heh), a shortcode in WordPress is a macro that can be used in the WYSIWYG editor. You add a shortcode to your post, and when WordPress renders the page, it swaps the shortcode for whatever code it’s a placeholder for, passing along any settings that have been included. WordPress has built-in shortcodes, like for the gallery, and shortcodes are also used in many plugins.

In the case of the WordPress gallery, if you wanted to insert one into a post, you would add the following shortcode to the editor:


Shortcodes, like HTML tags, can also be passed attributes — the gallery, like many shortcodes, has a number of settings you can tweak to customize it:

[gallery size="medium" columns="5" order="ASC"]

If you wanted to insert the gallery right into the WordPress theme rather than a post or page, you would just need to wrap the shortcode in the do_shortcode() function:

<?php do_shortcode('[gallery size="medium"]'); ?>

I didn’t know about this function until quite recently; my work-around was to add the shortcode to a WordPress page, and then query that page in the theme to display the content. Not very efficient!

Have you made any recent discoveries about WordPress that you wish you had known sooner? Please share them in the comments!

  1. […] week, we briefly touched on using shortcodes directly in your WP theme. In this week’s WordPress Wednesday, we’re going to cover how to build your own simple […]

    @ 9:10 am

Leave a Comment

Note: Fields marked with a * are required; email addresses are not published.

Legal FAQ Collections