Tag Archives: querystring

WordPress PHP: How to add URL Querystring Parameters to Your Plugin CodeUnit 26 DEC 2011

WordPress is set up such that unknown URL GET parameters are ignored, a nice security/navigation feature to be sure, but an annoying one if you are developing a plugin that needs to allow for GET variables in order to control your output.

Luckily there is a solution to achieve this though… to add new allowed URL parameters to your WordPress site, simply extend the query_vars filter! :)

In practice (you add this at the top of your plugin code):


function wtap_queryvars($qvars){
    return $qvars;

Note that it is a good idea to give your accepted GET parameters unique names so that you won’t clash with already accepted and processed GET variables. In the example above, I’ve gone and prefixed everything with wtap_.

Now that your site allows GET parameters, you obviously need to make use of them. To do this, first check their existence and then grab them via the wp_query global object:

global $wp_query;
if (isset($wp_query->query_vars['wtap_year'])){
echo '<p>Year passed in url: ' . $wp_query->query_vars['wtap_year'] . '</p>';

Great. So now http://yoursite.com/plugin-page/?wtap_year=2011&wtap_month=12 will throw up something valid, instead of an annoying 404 page!


Avoid HTML Special Characters in Your Querystring CodeUnit 13 MAR 2010

I must admit, I was totally left scratching my head a couple of days ago while presenting a survey capturing site I had just finished whipping up, when all of a sudden the site stopped redirecting properly when tested in Internet Explorer (IE) using compatibility mode. Completely flummoxed as to why something that worked perfect fine in some browsers simply didn’t work in others, I stopped to have a quick peek at what might be causing the problem and when I saw what it was, I couldn’t help but break out into laughter.

In order to redirect my script after successfully pushing data into the database, I was using Javascript to change the window’s location to a URL I was specifically building up with some required GET variables attached. However, my ordering of variables in the querystring couldn’t have been more unfortunate had it tried – you see, I was using a variable entitled reg and attaching it to the URL as &reg.

Now those of you with sharp eyes and a little bit of HTML knowledge will immediately slap your heads and go “duh” out loud, because yes, I had managed to accidentally declare a reserved HTML special character, namely &reg; which is of course translated to the registered trademark symbol. So my querystring was still working but with one less required GET variable as it was being lost in the redirect thanks to the older browser translating it into the actual symbol – hence causing that extreme sense of head-scratching puzzlement I had experienced earlier!

So be a pal and check your variable order in your querystrings carefully. You never know what you might inadvertently creating! :)