• Here’s the code

    add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);
    add_filter(‘query_vars’, ‘add_query_vars’);
    add_action( ‘wp_loaded’,’my_flush_rules’ );

    function my_flush_rules(){
    $rules = get_option( ‘rewrite_rules’ );

    if ( ! isset( $rules[‘(project)/(\d*)$’] ) ) {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    }
    }

    function add_query_vars($vars) {
    array_push($vars, ‘language’);
    return $vars;
    }

    function add_rewrite_rules($aRules) {
    $aNewRules = array(‘language/([^/]+)/?$’ => ‘index.php?pagename=home&language=$matches[1]’);
    $aRules = $aNewRules + $aRules;
    return $aRules;
    }

    Page Home is a static page set to be my homepage. The routing occurs correctly, but can’t retrieve the variable language from within page.php:

    if(isset($wp_query->query_vars[‘language’]))
    {
    $language = $wp_query->query_vars[‘language’];
    }

    However, if I change the rule above to another page other than my homepage, then I can retrieve the variable:

    $aNewRules = array(‘language/([^/]+)/?$’ => ‘index.php?pagename=intro&language=$matches[1]’);

    Any ideas why is this so?

  • The topic ‘Retrieving custom variable in query_vars’ is closed to new replies.