• 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 11 replies - 16 through 26 (of 26 total)
  • csam0003

    (@csam0003)

    thank you very much stvwlf, I am loading the twentyTen theme but also including my own custom one. Can you tell me exactly where u placed the function in the Twentyten functions.php?

    I apologise for the simple questions however I am new to all of this.

    thanks

    stvwlf

    (@stvwlf)

    Hi

    I placed it at the very bottom of functions.php

    No need to apologize. There is a lot to learn.

    Not sure what you meant by “also including my own custom one”. My suggestion is do your testing on the straight unmodified 2010 theme. If you see it working on the default theme and it doesn’t work on your theme then you know the problem is in your theme. If you mix and match 2010 with your theme and it doesn’t work you don’t know if the problem is the code itself or your theme or 2010.

    In WP the lowest common denominator, when everything fails, is the 2010 theme with all plugins turned off. That is straight out of the box WordPress. If it works then, you know WordPress is not the problem source. Then you turn on one plugin at a time until you find the source of the problem. If it works in the default theme with all the plugins on then you know the problem is in the other theme.

    Nothing about your case sounds like a plugin problem so you don’t need to do that part.

    csam0003

    (@csam0003)

    ok…so everything works where I have printed the value on the page template….Great.

    The next step is however including this code in my plugin which the page template uses on form submission. I have a feeling that the value in the wpquery is being reset or lost…

    csam0003

    (@csam0003)

    maybe I have to create a new query variable?

    stvwlf

    (@stvwlf)

    I’m not sure I understand what you are saying. You say you know what the value is within the page template. And that the page template calls a script on form submission. You can pass that value to the forms processing script as a hidden field in the form you are submitting.

    Am I not understanding something?

    csam0003

    (@csam0003)

    So in the page template I printed the value of the refID. So until that point it works. On the forms page however a number of fields are filled in and on clicking the submit button, a plugin is called which calls a function that puts together a url with the form data and (im hoping) the refID.

    function hook_ajax_register() {
    global $wp_query;
    $refId = $wp_query->query_vars['refId'];
    $this->marketing_code = $refId;
    
    $url_params = array(
    'lang' => $this->lang,
    'wlinfo' => $this->wlinfo,
    fname' => filter_var($data['clientFirstName'], FILTER_SANITIZE_STRING),
    'code' => $this->marketing_code,
    }
    
    etc...
    
    for some reason the marketing code is now returning an unknow value...
    If I directly print the value on the page template it works, however calling it in my plugin seems not to! So registering the variable is working, however getting the value in the plugin is not!
    csam0003

    (@csam0003)

    stvwlf

    (@stvwlf)

    I don’t get the relation between the page template, the forms page, and the plugin. Where is the forms page being activated in relation to the page template? Is the forms page called on the page template page? After? Before?

    csam0003

    (@csam0003)

    The issue that guy is having (link above) is the exact same problem.
    In the page template it works. In the plugin it doesnt…

    stvwlf

    (@stvwlf)

    You might want to ask your question here
    https://wordpress.stackexchange.com/

    csam0003

    (@csam0003)

    thanks alot stvWlf! U have been really helpful.
    What I did was get the value in the template as you suggested and then passed it to the plugin…
    This issue can be closed.

    thanks once again

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