WordPress.org

Ready to get started?Download WordPress

Forums

Retrieving custom variable in query_vars (1 post)

  1. danielcarrara
    Member
    Posted 3 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic