• Resolved gongwan33

    (@gongwan33)


    I have encountered a problem very similar to an old bug https://core.trac.wordpress.org/ticket/25143

    It happens again. I used the following code to register a query var ‘lang’. But after that, if I visited http://myhost.com/?lang=en , the front page was missing.

    function add_custom_page_variables( $public_query_vars ) {
    $public_query_vars[] = ‘lang’;

    return $public_query_vars;
    }
    add_filter( ‘query_vars’, array($this, ‘add_custom_page_variables’) );

    What happened? And how can I fix this?

    Thank you very much!

    • This topic was modified 7 years, 2 months ago by gongwan33.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It appears the patch was included in change set 32293 (v4.3). However, the patch only applies to using add_rewrite_endpoint() to manage query vars. Nothing was changed regarding the query_vars filter behavior on home pages.

    I advise you to use add_rewrite_endpoint() to add your query var instead of the query_vars filter.

    Thread Starter gongwan33

    (@gongwan33)

    Thank you very much for your reply.
    For some reason, add_rewrite_endpoint doesn’t work for my site. Finally I found a solution. It’s a little complicated, but it works.

    First, get the query var manually.
    $this->base_path = parse_url(get_site_url())[‘path’];
    preg_match(‘@’.$this->base_path.’\/(en|da|zh)\/?$@’, $_SERVER[‘REQUEST_URI’], $matches);

    $this->qv = $matches[1];

    Second, check the page type. If it is not the home page, add the customized query var. if it is the home page, DON’T ADD IT.
    if(empty($this->qv) && rtrim($this->base_path, ‘/’) != rtrim($_SERVER[‘REQUEST_URI’], ‘/’)){
    add_filter( ‘query_vars’, array($this, ‘add_custom_page_variables’) );

    }

    Finally, add the rewrite rules to validate the customized URL.
    global $wp_rewrite;
    $extra_rule[‘(‘.$this->langs_rex.’)/?$’] = ‘index.php?lang=$matches[1]’;
    $wp_rewrite->rules = $extra_rule + $wp_rewrite->rules;
    $wp_rewrite->flush_rules();

    The last but not least, before all of these steps, the URL autocompletion function must be turned off, or the page will be directed to somewhere else. To turn off the function, please refer to http://geekwagner.blogspot.com/2017/02/wordpress-about-url-autocompletion.html

    • This reply was modified 7 years, 2 months ago by gongwan33.
    • This reply was modified 7 years, 2 months ago by gongwan33.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘adding query var makes front page missing’ is closed to new replies.