Using custom URL parameters in Wordpress
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'];
}






















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
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
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
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
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
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
You, sir, are a lifesaver. Thank you
Comment by Sue — October 12, 2009 @ 10:26 pm
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
[...] 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
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
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
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
[...] 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
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
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