• Resolved ejankowski

    (@ejankowski)


    Hi there,

    I’m creating a plugin which need to register a new query var. I wrote this code in the main plugin file

    function add_my_query_var($vars) {
    
    $vars[] = 'my_var';
    return $vars;
    }
    
    add_filter('query_vars', 'add_my_query_var');

    But if I do, in any of my templates file, like page.php. There is no “my_var” .

    global $wp_query;
    var_dump($wp_query->query_vars);

    What am I doing wrong?

    Thanks!!

Viewing 15 replies - 1 through 15 (of 26 total)
  • You have to pass a value for my_var in before it will work

    What you’ve done so far is correct

    A) Add to functions.php

    add_filter('query_vars', 'lg_queryvars' );
    function lg_queryvars( $qvars ) {
      $qvars[] = 'productID';
      return $qvars;
    }

    b) pass the parameter on the URL in the link in the program where you need it
    <a href="/shop/?product_id=$variable_name">click here</a>

    c) in the shop page retrieve the query var value
    $productID = $wp_query->query_vars['productID'];

    Thread Starter ejankowski

    (@ejankowski)

    Thanks a lot for the answer!

    I was doing something wrong.

    Thanks!

    Hi, in the themes folder I have two functions.php files; where functions.php file includes functions-custom.php file.

    I placed

    add_filter('query_vars', 'lg_queryvars' );
    function lg_queryvars( $qvars ) {
      $qvars[] = 'productID';
      return $qvars;
    }

    in functions-custom.php file and in a custom plugin which a particular page uses , I included

    $productID = $wp_query->query_vars['productID'];

    I then have an external website that contains a link such as http://www.mysite.com/form/?product_id=24, however it seems not to work.

    Any ideas?

    You are setting up a variable called productID
    $qvars[] = 'productID';

    But you are passing on the URL a variable called product_id

    While similar (in English), to PHP productID and product_id are two different variables. Make them both the same and it should work.

    thank you
    yeah…still doesnt work…maybe I have to include the first part of the code in functions.php file and not functions-custom.php file?

    Should the classes.php file in the includes folder which contain such variables show that a query variable has been added?

    You just need to try different things and eliminate them one by one. The questions you are asking are theme-specific and depend on how the theme/framework you are using is set up.

    So you can conceptualize what is going on:

    $qvars is a global array that WordPress keeps that contains all the valid URL query strings that WordPress is interested in keeping. Basically WP deletes any query vars passed on the URL that it doesn’t find in that array. What you are attempting to do is add your desired query var to the array of query vars that WordPress knows about and keeps.

    When I was learning how to do this I found there is a second way to do it, which I found easier actually.
    http://www.simonwheatley.co.uk/2009/02/13/adding-get-params-to-a-url-in-wordpress/
    Maybe that alternate approach will help you.

    Thanks for your quick replies stvwlf!

    Should the classes.php file be affected when the new query variable is registered?

    I think you mean classes.php that is part of your theme. It shouldn’t affect anything but that is theme dependent. Go ahead and try it. Its’ not going to permanently change classes.php so if it breaks anything just take out the code you added.

    Also try the method in the article I linked to. It doesn’t require changing the query vars array.

    The thing is add_query_arg(); is used to add the parameters when knowing the value of the query.

    My situation is this. I have 3 external websites that are not wordpress. each website has a link to the same forms page on my wordpress site with a reference parameter…so

    external site 1 : http://www.mywpsite.com/forms/?refId=123
    external site 2 : http://www.mywpsite.com/forms/?refId=456
    external site 3: http://www.mywpsite.com/forms/?refId=789

    On the forms page, the fields are inputted and on submit a plugin is called which takes the form fields aswell as the refID and populates an external database.

    Is there a way of checking in some file if the query variable has been registered?

    I just tested this and it worked fine.

    your code in functions.php should be

    add_filter('query_vars', 'my_queryvars' );
    function my_queryvars( $qvars ) {
      $qvars[] = 'refId';
      return $qvars;
    }

    I put
    <?php var_dump($wp_query->query_vars); ?>
    near the top of the page template that is used to display the page at URL http://www.mywpsite.com/forms/?refId=123

    When I viewed a URL that had ?refId=123 at the end of the URL, the first item in the array that displayed was

    array(56) {
      ["refId"]=>
      string(3) "123"

    you can access the value of the query variable like this
    $refId = $wp_query->query_vars['refId'];

    If I don’t have a query string on a URL, refId is not in the array. Its only appearing when it is passed on the URL.

    Does this help?

    mmmm interesting…first of thank you very much for your help. Im a wordpress newbie.

    I included <?php var_dump($wp_query->query_vars); ?> at the top of the template and getting NULL value

    just a quick question should I declare $wp_query as global in the page template?

    Depends where in what template file you put it in. I placed it in index.php after the #content div and didn’t need to declare it as global.

    No harm done declaring it as global – that would be the next thing to try

    yeah…I think its just not registering for some strange reason.
    The fact that is NULL means that I guess its not finding the query variable..
    Im lost 🙁

    the var dump is just to confirm that the value is being picked up. it has nothing to do with whether the function is working.

    The issue is are you seeing a value in $wp_query->query_vars[‘refId’] when you use it on a page with the URL
    http://www.mywpsite.com/forms/?refId=123

    If the answer is No, I suggest you temporarily load the TwentyTen default theme – add the function to its functions.php file and call the same URL. See if it is working in the default theme. If it works there and not in your theme you know the issue is your theme. Then you can contact the theme author for suggestions.

    I used the TwentyTen theme here when I was testing and I assure you it worked exactly as I said.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘query_vars filter doesn't work (?)’ is closed to new replies.