Have you ever tried to tweak your WordPress website in a way that requires you to pass parameters to a page? Perhaps you have tried to embed a piece of 3rd-party code into your page and found that it doesn’t work because the code requires a URL parameter to have been passed to it – and the parameter seems to have vanished as the page loads?
The reason for this is that WordPress automatically strips out parameters it doesn’t recognise before it builds your pages. The solution is to trick WordPress into accepting your parameters as though they were integral to the framework. I couldn’t find an ‘official’ WordPress plugin to allow me to do this but came across this solution (credits below)…
You will need to edit the template file which loads the page you want to receive the parameter, and you will need to upload some code as a plugin to name the variable(s) that you want to use, but it seems to work well.
Here is the plugin code, which needs to get uploaded to your plugins folder and activated…
<?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[] = 'yourvarname';
return $qvars;
}
?>
…and here is an example of how to use it on your template page…
global $wp_query;
if (isset($wp_query->query_vars['yourvarname']))
{
print $wp_query->query_vars['yourvarname'];
}
You need to change yourvarname to the name of the variable your page expects to receive.
Credits
Thanks to webopius.com for pointing us to the solution to this problem.
If this was useful to you, please leave us a comment or like/share it with your social media networks. This encourages us to post more useful code snippets and website fixes.
We do a lot of work on WordPress sites, so if you have a tricky problem you’re trying to resolve please get in touch and we’ll help out if we can.
Hi
Thanks for this idea. It’s a neat way of passing arguments in wordpress. I used it for a training website I was working on (fourstepstraining.com) to customise the contact us form depending on which page it was clicked from. It works a treat.
Hi,
You said in the article I need to change ” yourvarname to the name of the variable your page expects to receive” — where do I find this?
Thanks.
Hi Daniel.
Your variable name should be apparent to you from the parameter string that you’re trying to read in your WordPress page. For example, if your parameter was something like ‘yourdomainname.com/?searchparam=xyz’ then the variable name would be ‘searchparam’.
Hi, what is I have more than 1 var I want to pass, like name and idm can I just add them in the php and template?
yhx
Hi Didier.
It’s quite a while since I wrote this post, so I’m not 100% sure but I think you would just repeat the code for each parameter, something like this…
add_filter(‘query_vars’, ‘parameter_queryvars’ );
add_filter(‘query_vars’, ‘parameter_queryvars1’ );
function parameter_queryvars( $qvars )
{
$qvars[] = ‘myparam1’;
return $qvars;
}
function parameter_queryvars1( $qvars )
{
$qvars[] = ‘myparam2’;
return $qvars;
}
I had the same issue and I found that this proposed fix didn’t work. I eventually succeeded by editing the plugin code thus:
where param1 and param2 are the two variable names to be passed.
Great solution for passing one parameter. Thanks. But I am trying to get it to work with multiple parameters. “Paul” seems to have made it work for him but unfortunately Paul you didn’t provide how to process it on the target page and I am having difficulty figuring it out. Can anyone help please, and thanks.
I’m not sure I understand, how and where exactly would I add the second bit of code – …and here is an example of how to use it in your template page….
Would I add that code in the full-width.php page template, if that is the template I chose for my pages? Where exactly would I add it? Would that bit of code get wrapped in php tags?
Thanks in advance –
Hi Jack.
You would add that code to your template file (full-width.php in your case) or alternatively in your theme functions.php file. I guess it doesn’t really matter where you put the code, as long as it is before the point where you need to reference the passed parameters.
The code needs to be wrapped in tags, yes.
So I could put both sets of code in the functions.php file, the first set of code as well as the second set? I will try that and see, thank you.
Wonderful! This is working very well.
Thanks for posting this! There is now a plugin that offers this functionality, called URL Params. Cheers, Graham
That’s a good shout Graham, thanks for letting us know. I wrote this post some years ago at a time when I don’t think this plugin was around. For other readers of this thread, I think the plugin in question is this one.
How would I rewrite the URL in wordpress to append the new variable to every page in the permalink style.
example:
mysite.com/anypage/ibo/1234
would actualy resolve to:
mysite.com/anypage?ibo=1234