Tag Archives: query_vars

How to add Custom Query Parameters in WordPress Programming 08 JUL 2013

wordpress-logoI’m in the process of modifying my WordPress theme such that when you click on one of the post excerpts shown on the home page, or any category page for that matter, the resulting post page shows up in a fancybox lightbox, thereby allowing you to safely and quickly open up links without worrying about losing your place on the scrolling listing page.

In order to minimize the work for myself, I intend to reuse the single.php file in my theme to display for both a standard accessed single page as well as this new lightbox view, though obviously the two views would have to be tweaked, as for example on the lightbox page, you don’t necessarily want to show the site’s header all over again!

So how does one do this? Well, the obvious answer is that we need either a $_GET or $_POST variable which we can use to distinguish the two views, which brings us to the next question: how does one add a custom query parameter to WordPress such that it’s rewrite engine understands what to do with the new parameter?

Well the answer lies in the useful query_vars filter which WordPress uses in its URL rewriting mechanism.

Locate or create the functions.php file in the root of your theme folder and add the following bit of example code:

add_filter('query_vars', 'fancybox_display_queryvars' );

function fancybox_display_queryvars( $qvars )
{
  $qvars[] = 'fancybox_display';
  return $qvars;
}

In this particular example I’m only adding one new URL parameter (fancybox_display) to those that WordPress already understands, meaning that given any URL that is suffixed with ‘?fancybox_display=1’ will result in a $_GET variable I can access and make use of.

For example, to check if it exists, and thus know we need to follow the logic dictating a lightbox layout using our example above, use the following:

if( isset( $wp_query->query_vars['fancybox_display'] )) {
 //do something 
}

And that’s all there is to it really.

UPDATE: Only noticed now that I’ve obviously stumbled into this problem before… Oh well, a double post shouldn’t hurt me too much I guess.

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):

add_filter('query_vars','wtap_queryvars');

function wtap_queryvars($qvars){
    $qvars[]='wtap_month';
    $qvars[]='wtap_year';
    $qvars[]='wtap_categories';
    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!

Nifty.