• Hi,

    I have a question regarding setting up my own get vars in WP and grabing them while using the pretty permalink structure.

    I know that there have been some topics about it and even tutorials on the web. But I still cant manage to get it to work so I wanted to ask for help with my example.

    So what I did.
    First:
    regster the new variable ‘nng_users’:

    function nng_users_query_vars( $vars ) {
        array( $vars, 'nng_users' );
        return $vars;
    }
    add_filter('query_vars', 'nng_users_query_vars');

    Second:
    adding rewrite rules:

    function nng_users_rewrite_rules( $rules ) {
    	$newrules = array( "benutzer/([^/]+)/?$" => "index.php?pagename=user&nng_users=$matches[1]" );
    	$finalrules = $newrules + $rules;
            return $finalrules;
    }
    add_filter('rewrite_rules_array','nng_users_rewrite_rules');

    So for my understanding: WordPress _should_ recognize the nng_users variable which is submitted whenever an url goes like https://www.xyz.de/benutzer/whatev/
    And in this particular case, it should call the page with the slug “user”.

    But it just doesn’t seem to register the damn nng_users variable. When I type e.g. /benutzer/register/ it calls the user page, which is correct. But I the $wp_query->query_vars array does not hold the nng_users variable… Am I missing something?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You are creating an oddly structured array in your ‘query_vars’ filter function but not assigning it to a variable. Then you return $vars unchanged. I’m guessing at how $vars is structured, but you probably meant to do something more like this:

    function nng_users_query_vars( $vars ) {
        array_push( $vars, 'nng_users' );
        return $vars;
    }

    Thread Starter Michael

    (@ms-s)

    argh I’m an idiot…

    of course I wanted to array_push.. stupid me, thank you very much.

    FYI:

    I don’t really know why, but it seems that for the rewrite, you need to use single quotes. At least for me
    $newrules = array( "benutzer/([^/]+)/?$" => "index.php?pagename=user&nng_users=$matches[1]" );
    does not work but
    $newrules = array( 'benutzer/([^/]+)/?$' => 'index.php?pagename=user&nng_users=$matches[1]' );
    does.

    Moderator bcworkz

    (@bcworkz)

    Can’t tell you how many hours I wasted looking for silly errors like that.

    PHP tries to expand variables in dbl. quotes. Since $matches[1] is not defined in your function, with dbl. quotes, you’re probably getting nng_users=NaN.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘query_vars and permalinks question’ is closed to new replies.