WordPress : Pass custom variable as URL parameter

Sometimes you need a simple way of passing a custom URL variable to WordPress. WordPress has to be quite organised in this respect as it passes a lot of arguments back and forth, so unless you want to grub around directly with the $_GET array, then this solution is a little more elegant.

The first thing to do is tell WordPress to expect your query variable. WordPress keeps track of the query variables it is expecting in $wp_query->query_vars array.You can stick whatever you want onto the end of your URL and WordPress will disregard it unless it is expecting it. Fortunately, WordPress has a filter which allows the query_vars array to be manipulated before WordPress processes any URL parameters.

You can register, for want of a better term, a new variable by adding this code to your theme’s functions.php file.

This example will allow you to add a URL parameter value myvar to your request URL. For example, you might have a landing page for a marketing drive, and you want to know which prospect has clicked on a link in their email:


So, how to we get at the value in the myvar variable? Just check that get_query_var(‘myvar’) returns a value. So, for example if you wanted to detect this within a blog post, then just put a test like this into the theme’s single.php file.

One Response to WordPress : Pass custom variable as URL parameter

  1. Adam September 2, 2015 at 15:02 #

    Thank you! The simplest explanation I’ve seen on how to pass custom variables in WordPress.

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.