December 1, 2008

Using custom URL parameters in WordPress

Filed under: Tips — Tags: , — Webopius @ 1:32 pm

Sometimes when you are building WordPress sites you need to pass a parameter via the URL like this:

http://www.mysite.com?myvar=222

The problem is that WordPress is designed to reject any URL query parameters that it doesn’t recognise so your URL parameter will be dropped before you get a chance to use it.

The solution

Step 1

We need to tell WordPress about the new parameter(s) we will be sending via the URL. We want WordPress to recognise any url parameter sent in the format ‘http://www.yoursite.com?myvar=hello’ in any page on our WordPress site.

The easiest way to do this is to create a WordPress plugin that uses a query filter to tell WordPress about new parameters. Here’s the plugin code:

<?php
/* Plugin Name: Parameter
Plugin URI: http://webopius.com/
Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress
Author: Adam Boyse
Version: 1.0
Author URI: http://www.webopius.com/
*/
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = ' myvar';
return $qvars;
}
?>

The plugin is configured to add one new URL parameter name ‘myvar’ to WordPress. Just copy the above code to a new .php file which you then copy to the plugins directory of your WordPress install. You then need to activate the new plugin from within your WordPress admin screens.

Step 2.

Now from any WordPress page that you can add code to (e.g. Theme page) or your own standalone page that is WordPress aware you can use your variable like this:


global $wp_query;
if (isset($wp_query->query_vars['myvar']))
{
print $wp_query->query_vars['myvar'];
}


Add to Technorati Favorites

27 Comments »

  1. I’m having a problem with Step 2 and would appreciate if you could point me in the right direction by telling me where to add that code.

    Comment by Liam — March 6, 2009 @ 5:12 pm

  2. Hi Liam,

    What problem are you having? If you email me directly, I’ll try and help: info@webopius.com

    Comment by Webopius — March 6, 2009 @ 5:47 pm

  3. Hi, your code works great but is there any way I can make WordPress append my variable to other links so that if the visitor navigates to another page on the site, I can still use my variable?

    e.g. http://www.mysite.com/contact?myvar=hello

    Thanks.

    Comment by onedollar — April 18, 2009 @ 2:13 pm

  4. If you wanted to add the param to all post links, one way to do this would be to write a new filter for post_link and change the url’s dynamically. See this page in the WordPress API for other link filters:

    http://codex.wordpress.org/Plugin_API/Filter_Reference#Link_Filters

    Comment by Webopius — April 18, 2009 @ 5:49 pm

  5. Why can’t you use:

    if (isset($_GET['myvar']))

    That seems to work for simply checking if a there’s a parameter in a URL

    Comment by Stephen Cronin — June 10, 2009 @ 7:45 am

  6. You have a space in the quotes on the line:
    $qvars[] = ‘ myvar’;

    Which will cause confusion…. :)

    Comment by Tim — June 18, 2009 @ 6:25 pm

  7. You, sir, are a lifesaver. Thank you :-D

    Comment by Sue — October 12, 2009 @ 10:26 pm

  8. I recently had a situation in which I needed to pass a URL parameter into WordPress so that it could be inserted into all links off of a blog page. For example, a partner would pass traffic to a blog using the format http://www.myblog.com?refcode=joe. All links embedded in the post would need to pass the value of refcode if it existed, even if the visitor requested multiple pages after the initial visit. You can get plugin that will do this at:

    http://www.z-car.com/blog/2009/12/05/cookie-monster-wordpress-url-parameter-utility

    Comment by gary — December 9, 2009 @ 9:37 pm

  9. [...] Using custom URL parameters in WordPress ~ Webopius WordPress is designed to reject any URL query parameters that it doesn’t recognise so your URL parameter will be dropped before you get a chance to use it. [...]

    Pingback by Passing vars in URL within WordPress « Web Page Authority Blog — December 28, 2009 @ 6:01 pm

  10. [...] investigations led me to a post by Webopius, discussing custom URL paramaters in WordPress – which has proved incredibly [...]

    Pingback by Passing variables via URL to Wordpress Posts and Pages | thewordpresswarrior.com — February 16, 2010 @ 12:08 pm

  11. This method works great for me BUT, what if I want to add more than one variable to be recognized by wordpress? I can’t seem to adapt the plugin code in a way that will work.

    Comment by Mike — March 25, 2010 @ 8:03 pm

  12. Try this Mike:
    http://www.z-car.com/blog/2009/12/05/cookie-monster-wordpress-url-parameter-utility?refcode=CookieMonster&refcode2=cookie2

    Both refcode and refcode2 are available to be used as a short-code in your post. Make sure you go to settings and enter your two parameters that you want to use. I have tested two, but it should work for an unlimited number.

    Comment by Gary — March 26, 2010 @ 5:08 pm

  13. i dont know how to use it really

    my situation now i have generated a link in a page with a param
    http://www.example.com/blog/?user_name=joe
    i need to change it to be
    http://www.example.com/blog/joe
    and also send the param to the other page
    how i can do this please help me :)

    Comment by hiedar — March 27, 2010 @ 2:14 pm

  14. update the post and remove the blank space in the ‘ myvar’
    thanks! great post!

    Comment by linus — March 31, 2010 @ 12:13 pm

  15. [...] http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress [...]

    Pingback by Passing url parameters to a Wordpress page | Website and Software Services — May 20, 2010 @ 12:19 pm

  16. I’ve been trying to work with your plugin for a while. I’ve got a page named foreclosure and have a list of 1000 properties from my dbase. Each property has a short description, but I wanted to create a link to a detailed page for each property. I’m having a heck of a time getting each link to go to my template page and to pass the variable in the URL.

    echo ““. “More Detail>>” . ““,”\n”;

    Then on my detail page, I’m using the following code:
    $result = mysql_query(“select * from `bankowned` where `list number` = ‘”.mysql_real_escape_string($_GET["myvar"]).”‘”) or die(mysql_error());

    This config worked great on one of my other sites – but not on WordPress. Can you straighten me out?

    Comment by Troy — July 15, 2010 @ 11:24 am

  17. Hello, I am having some problem with my wordpress site http://www.webhealthcare.info I am new in wordpress and my knowledge in programming is very less. I just want to why I am failing to find my articles with an param link extension of http://www.webhealthcare.info/diabetes/abc/xyz.html
    Thanks

    Comment by manidip bandyopadhyay — August 24, 2010 @ 10:35 am

  18. Hi,

    I have tried your way, but there never ended page on my page.

    I tried to by pass my old php page to wordpress.
    mydomain.com/weblinks.php?cat_id=1&weblink_id=3

    Can you give me a solution

    Comment by Hari — November 1, 2010 @ 12:48 am

  19. Is there a way to pass URL variables with permalinks enabled?

    Comment by David Lewis — December 4, 2010 @ 12:13 am

  20. Opps. I see now that you can just append your new variable to the end of the permalink… as in…

    http://www.mydomain.com/some/pretty/permalink/?myvar=foobar

    …and WordPress accepts it. Thanks!!!!!

    Comment by David Lewis — December 4, 2010 @ 12:19 am

  21. Oops! I see now that you can just append your new variable to the end of a permalink and WordPress accepts it. Awesome! Thanks!

    Comment by David Lewis — December 4, 2010 @ 12:20 am

  22. Thanks so much for this. Its working for my regular pages, but i also need to pass a parameter to an admin page – thats nto workign out for me. Any ideas why not?

    Comment by Judy — December 17, 2010 @ 4:06 am

  23. If you want to pass more parameters just add them to the qvars array

    add_filter(‘query_vars’, ‘parameter_queryvars’ );
    function parameter_queryvars( $qvars )
    {
    $qvars[] = ‘myfirstvar’;
    $qvars[] = ‘mysecondvar’;
    return $qvars;
    }

    In step 2 they will end up in

    $wp_query->query_vars['myfirstvar']
    $wp_query->query_vars['mysecondvar']

    Comment by Hans — February 18, 2011 @ 10:38 am

  24. I am new to WordPress and for some reason I can’t seem to get this to work.
    1. I got the plugin installed…
    2. Where does the $wp-query go in my wp template page – before the loop in the loop…
    3. Can I send a variable and what is the syntax

    $mysidebar = get_post_meta($post->ID, ‘sbg_selected_sidebar’, true);

    would the below be the correct syntax to append and do I add this to the end of my permalink in the permalink section?

    ?myvar=$mysidebar

    where would I put the if (isset($_GET['myvar'])) i.e. can I put it in another template page and grab the url variable?

    Any help would be greatly appreciated.

    Tammy

    Comment by tammy — April 6, 2011 @ 8:12 pm

  25. Hi,

    I am having a .php file inside themes/themename/ folder.Inside that i want to take my new URL parameter.For that, I added the above mentioned plugin and activated it..But there is no effect.I checked the same query string in some other folders/files also.But no where it is getting printed..Please help me.Dont know What I am doing wrong.

    Comment by sheeba — April 19, 2011 @ 9:16 am

  26. Hi i don’t think this works anymore, i was passing parameters perfectly until a recent update i’m not sure which wordpress broke this though.

    Comment by scott — April 29, 2011 @ 5:25 am

  27. Thank you! Thank you! Thank you! I’ve been looking around on the web for weeks for this solution.

    Comment by Clarence — June 19, 2011 @ 3:06 am

RSS feed for comments on this post. TrackBack URL

Leave a comment