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.

First, I register a variable named "bouquet" using this function & this filter:

add_filter( 'query_vars', 'bouquet_query_vars' );
function bouquet_query_vars( $query_vars )
{
   $query_vars[] = 'bouquet';
   return $query_vars;
}

Next, I add my rewrite rule. www.mysite.com/les-contenus/les-bouquets/bouquet/test/ should become www.mysite.com/les-contenus/les-bouquets/?bouquet=test

add_filter('rewrite_rules_array', 'bouquet_rewrite_rules');
function bouquet_rewrite_rules($rules) {
   $new_rules = array('bouquet/([^/]+)/?$' => 'index.php?pagename=les-bouquets&bouquet=$matches[1]&pagename=les-bouquets');
   $rules = $new_rules + $rules;
   return $rules;
}

To test if my script can get the var from the url I do:

add_action( 'template_redirect', 'get_bouquet');
function get_bouquet() {
    global $wp_query;

    if(isset($wp_query->query_vars['bouquet'])) {
        $bouquet = get_query_var('bouquet');
        echo "The amazing bouquet is ".$bouquet;
    }else{
        echo "var not set";
    }
}

I use the plugin Rewrite analyzer and my rule is detected. But when I go on my page with the url /les-contenus/les-bouquets/bouquet/test/ the url become /les-contenus/les-bouquets/ and my var is not set... And when I test /les-contenus/les-bouquets/?bouquet=test it works. It's very strange. So my query_var works, the custom url is detected in the analyzer but the var is not set when i test my url.

Please help.

share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.