Lessons Learned: How Not to Name Shortcode Attributes in WordPress

Developing my first WordPress plugin provided me with many learning opportunities. We already talked about the quicker way of uploading plugin files. In this post, I wanted to share the problem I had with shortcode arguments. Hopefully it will help you avoid the same issue.

Background

To pass information through a WordPress Shortcode, attributes can be added to the shortcode tag. The following shortcode, for example, has an attribute called "cat" which is assigned the value "FAQ":

[postlist cat="FAQ"]

The function that processes the shortcode can then access the attribute value using the following argument variable:

$args['cat']

The Problem

I wanted to name the attributes / arguments as I do for other PHP scripts. My original shortcode was going to be something like the following:

[postlist catID=14]

But the $args['catID'] variable didn't contain any data. After making sure the plugin file was uploaded to the correct folder, double checking spelling, and everything else I could think of to get the shortcode working…I finally check the contents of the $args variable.

print '<pre>' . print_r($args, true) . '</pre>';

It turns out that WordPress automatically lower cases the argument names. To access the value, I needed to use

$args['catid']

Related Posts

0 Comments

There are currently no comments.

Leave a Comment