Passing url parameters to a WordPress page

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 in 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 at the solution to this problem.

If this was useful to you, please leave us a comment or like / share 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, and so if you have a tricky problem you’re trying to resolve please get in touch and we’ll help out if we can.

Tags: , , ,

Categories: What I have learned today, Wordpress

11 Responses to “Passing url parameters to a WordPress page”

  1. Jon Davies says:

    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.

  2. Daniel says:

    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’.

      • didier says:

        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;
          }

          • Paul says:

            I had the same issue and I found that this proposed fix didn’t work. I eventually succeeded by editing the plugin code thus:

            PHP Code to pass parameters to WordPress pages

            where param1 and param2 are the two variable names to be passed.

  3. jack says:

    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 < ?php ...... ?> tags, yes.

  4. Jack says:

    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.

  5. Niranjana says:

    <a href="/?myValue=”>title;?>

    this is my link and when it clicked it goes to Details page. But i can get the MyVal Parameter value. Please help me

  6. Fritz Salzmann says:

    Wonderful! This is working very well.

Leave a Reply

Share This

Please share if you've liked reading this

Share this post with your friends! It really helps us, and lets us know which content visitors like best - so we can write more like that!