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
<?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!