Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a set of variable set up, created with add_filter in my plugin functions file:

    add_filter('query_vars', 'user_country_var' );
add_filter('query_vars', 'user_lat_var' );
add_filter('query_vars', 'user_lon_var' );

This is then referenced by some code which I used to process the filters. This worked fine when the code was just chucked into a template file. I'm trying to tidy things up and I'm stumped.

I've put the following function in a class, it's currently called at init, and is definitely there (sits alongside some other code that works fine). But it's just not getting the variables from my url, and so is outputting the fallback, 'your country':

// pull variable from url
if (isset($wp_query->query_vars['country']))
    {
    $this->user_country = $wp_query->query_vars['country'];
    }
    else { $this->user_country = 'your country'; }


if (isset($wp_query->query_vars['lat']))
    {
    $this->user_lat = $wp_query->query_vars['lat'];
    }
    else { $this->user_lat = '52.5'; }

    if (isset($wp_query->query_vars['lon']))
        {
        $this->user_lon = $wp_query->query_vars['lon'];
        }
        else { $this->user_lon = '13.4'; }

Is there any reason this shouldn't be able to get the variables? As I said, it sets the default of the class's public variables fine and is executed. It's just not getting the variables even when they're there...

I can post more code if it helps, or indeed clarify. Thanks in advance for any ideas!

share|improve this question
    
I've figured it, but I can't post my answer yet! basically I forgot to declare $wp_query as a global variable to use the filters within my class. DOH. I'll post code when I'm allowed. –  josh Dec 30 '11 at 19:27

1 Answer 1

up vote 0 down vote accepted

As I said, I was just missing the declaration to use the global $wp_query, so it wasn't accessing my filters.

Here's what worked incase it helps:

global $wp_query;
// pull variable from url
    if (isset($wp_query->query_vars['country']))
        {
        $this->user_country = $wp_query->query_vars['country'];
        }
        else { $this->user_country = 'your country'; }


    if (isset($wp_query->query_vars['lat']))
        {
        $this->user_lat = $wp_query->query_vars['lat'];
        }
        else { $this->user_lat = '52.5'; }

        if (isset($wp_query->query_vars['lon']))
            {
            $this->user_lon = $wp_query->query_vars['lon'];
            }
            else { $this->user_lon = '13.4'; }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.