WordPress Shortcodes… in short

Digg Muti Delicious

In the spirit of the topic, I’ll keep this post short and sweet. Today I’ll be dis­cuss­ing WordPress’s short­code API and how to util­ise it.

WordPress short­codes are codes (eg: [list_​bookmarks]) which can be typed into the con­tent area of a page or post in order to provide func­tion­al­ity of some kind. Shortcodes can be wrapped around text (sim­il­arly to how HTML tags are wrapped around text) or can be used as a single tag. In this post, I will dis­cuss using short­codes as a single tag to save time as well as provid­ing more con­trol to the user of your WordPress theme/​plugin/​function.

A short­code con­sists, in it’s most basic form, of two ele­ments: a PHP func­tion and a line to add the short­code into the sys­tem. A basic struc­ture would like like this:

function list_bookmarks() {
INSERT CODE HERE
}

add_shortcode('list_bookmarks', 'list_bookmarks');

The call to the short­code would look like this:

[list_​bookmarks]

To provide a bit of clar­ity to the above, the add_shortcode func­tion has two attrib­utes: the first being the code you want to be the short­code and the second being the name of the func­tion called when the short­code is typed.

Another really use­ful aspect of WordPress short­codes is the $atts attrib­ute passed to the func­tion. This is an optional para­meter which can be included within the func­tion in order to pass attrib­utes through the short­code. An example of the above with the $atts para­meter would look like this:

function list_bookmarks($atts) {

extract($atts);

INSERT CODE HERE
}

add_shortcode('list_bookmarks', 'list_bookmarks');

The call to the short­code could now look like this:

[list_​bookmarks category=“Uncategorised”]

The above will then pass a vari­able called $category inside the func­tion, with the value “Uncategorised”.

The above code now looks for attrib­utes passed through the short­code and cre­ates a vari­able out of each (using the PHP ‘extract’ func­tion). If no attrib­utes are passed through, your func­tion will throw an error.

One thing to keep in mind with WordPress short­codes is that when dis­play­ing inform­a­tion, it is recom­men­ded that one uses return instead of echo to dis­play the information.

Your short­code could per­form any func­tion you desire, from dis­play­ing a single line of text to cre­at­ing a large page of con­tent with func­tion­al­ity, data­base quer­ies and more. This is also a great way to be able to re-​​use code, as well as be able to shirt con­tent around your web­site with just the move of a single string of text.

I hope this post has been inform­at­ive and use­ful. (make sure to check any nat­ive WordPress short­codes so as to not cre­ate short­codes with names that have already been used in WordPress, eg: gallery).

For more on WordPress short­codes, check out the WordPress Codex Shortcode API.

Related Posts

Leave your comment

Your email is never shared. Required fields are marked *

*
*