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'];
}
Tags 3d Add new tag apple asp aspdotnetstorefront backups bugzilla cgi chrome cloud computing cms content management CRM css Document Management drupal ecommerce expression engine flash google hiring host hosting img_assist interprise JQuery KnowledgeTree mac mamp modules mouse nginx opencart php PRINCE2 printer project management snow leopard ssl tinymce Tips translation unix webgains wordpress
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.
Hi Liam,
What problem are you having? If you email me directly, I’ll try and help: info@webopius.com
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.
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
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
You have a space in the quotes on the line:
$qvars[] = ‘ myvar’;
Which will cause confusion…. 🙂
You, sir, are a lifesaver. Thank you 😀
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
[…] 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. […]
[…] investigations led me to a post by Webopius, discussing custom URL paramaters in WordPress – which has proved incredibly […]
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.
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.
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 🙂
update the post and remove the blank space in the ‘ myvar’
thanks! great post!
[…] http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress […]
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?
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
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
Is there a way to pass URL variables with permalinks enabled?
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!!!!!
Oops! I see now that you can just append your new variable to the end of a permalink and WordPress accepts it. Awesome! Thanks!
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?
[…] Zdroj: http://www.webopius.com […]
Hi,
For Step 2 I included the code in my custom plugin php file:
$this->referencecode = $wp_query->query_vars[‘myvar’];
however I am not getting anything?
any ideas?
thanks
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’]
Hey,
I have a inquiry for the webmaster/admin here at http://www.webopius.com.
May I use part of the information from this post above if I provide a backlink back to your site?
Thanks,
Peter
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
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.
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.
Thank you! Thank you! Thank you! I’ve been looking around on the web for weeks for this solution.
Does this work with more than three parameters?
Yes, it does work for more than three parameters. Thanks for writing such a useful plugin. 🙂
Can’t make it work for 2 parameters, here’s my plugin code:
And the code i’m using to call the variables:
query_vars[‘pa’];
print $wp_query->query_vars[‘ar’];
?>
This is what i’m using then:
http://www.mysite.com/?pa=first&ar=second
This solution worked for me perfectly. However, I need to set a page on my wordpress as a “static page” (front page). As soon as I do that through “reading” under “setting” in admin panel, this stops working. Meaning that when I enter http://www.domain.com/?refid=xyz , it returns “post not found”, whereas http://www.domain.com by itself works.
Anyone any ideas how I can fix that?
Hi. I am not a programmer and I have a question. I have a IJS i-frame fom indeed jobs. There is one link to post a job that is supposed to open up on an extenal site but it doesn’t work and just redirects to the same exact page the person is on. I wrote for help and was told that, The link appends variables to the url of the page, and WP won’t allow that to happen
Is there anything I can do so that it will work?
Thanks a bunch,
[…] added a querystring var to wordpress like this and now I need to change the notation from ? to slashes, so […]
I’m surprised that no one seems to know any reason not to use _GET instead?
I think there’s one reason to sometimes *do* use _GET though, namely if you switch theme based on a URL query string parameter. Theme switching would happen fairly early, and it seems to me that $wp_query->query_var hasn’t been inited at that time.
[…] http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress See: […]
In step 2 you can do:
$myvar = get_query_var( ‘myvar’ );