最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - How to remove query string from current page URL?

programmeradmin6浏览0评论

On page load, I wanted to remove specific query string from the current URL, is that possible? Is there a specific function for that?

There is a function remove_query_arg('query_key'); and template_redirect, I think I can use them, something like.

function abc_redirections() {
    $url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (isset($_GET['query_string'])){
        if(empty($_GET['query_string']) || !is_numeric($_GET['query_string']) || $_GET['query_string'] < 1){
            $url = remove_query_arg('query_string', $url);
        }
    }
    if (isset($_GET['query_string_1'])){
        if(empty($_GET['query_string_1']) || !is_numeric($_GET['query_string_1']) || $_GET['query_string_1'] < 1){
            $url = remove_query_arg('query_string_1', $url);
        }
    }
    
    if(isset($_GET['query_string_1']) || isset($_GET['query_string_1'])){
        wp_redirect($url);
        exit;
    }
}
add_action( 'template_redirect', 'abc_redirections' );

But it won't work, the page says "Page not working, redirected you too many times.". Something's not right with the condition. Also, I'm not sure if that's the correct way to get the current page URL. Another thing, isn't it too bad to do this on template_redirect? I mean, the page is already fetched and then it redirects again if the condition is met, feels like not clean for me.

On page load, I wanted to remove specific query string from the current URL, is that possible? Is there a specific function for that?

There is a function remove_query_arg('query_key'); and template_redirect, I think I can use them, something like.

function abc_redirections() {
    $url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (isset($_GET['query_string'])){
        if(empty($_GET['query_string']) || !is_numeric($_GET['query_string']) || $_GET['query_string'] < 1){
            $url = remove_query_arg('query_string', $url);
        }
    }
    if (isset($_GET['query_string_1'])){
        if(empty($_GET['query_string_1']) || !is_numeric($_GET['query_string_1']) || $_GET['query_string_1'] < 1){
            $url = remove_query_arg('query_string_1', $url);
        }
    }
    
    if(isset($_GET['query_string_1']) || isset($_GET['query_string_1'])){
        wp_redirect($url);
        exit;
    }
}
add_action( 'template_redirect', 'abc_redirections' );

But it won't work, the page says "Page not working, redirected you too many times.". Something's not right with the condition. Also, I'm not sure if that's the correct way to get the current page URL. Another thing, isn't it too bad to do this on template_redirect? I mean, the page is already fetched and then it redirects again if the condition is met, feels like not clean for me.

Share Improve this question edited Jul 29, 2020 at 2:06 Polar asked Jul 28, 2020 at 8:49 PolarPolar 1311 silver badge13 bronze badges 2
  • 1 So what's your question? Your approach looks fine and the docs for remove_query_arg look pretty clear? – mozboz Commented Jul 28, 2020 at 10:09
  • @mozboz - I decided to try my luck and do it on my own, but still having an issue. Please see the updated post. – Polar Commented Jul 29, 2020 at 2:07
Add a comment  | 

1 Answer 1

Reset to default 0

You don't need $url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; for getting the current URL to remove the query string.

All you need is remove_query_arg('the_query', false). False, means it will use the current URL and your code can be shorten to:

function abc_redirections(){
    if(isset($_GET['query_string']) || isset($_GET['query_string_1'])){
        $array = array();
        if(isset($_GET['query_string']) && (empty($_GET['query_string']) || !is_numeric($_GET['query_string']) || (is_numeric($_GET['query_string']) && $_GET['query_string'] < 1))){
            $array[] = "query_string";
        }
        
        if(isset($_GET['query_string_1']) && (empty($_GET['query_string_1']) || !is_numeric($_GET['query_string_1']) || (is_numeric($_GET['query_string_1']) && $_GET['query_string_1'] < 1))){
            $array[] = "query_string_1";
        }

        wp_redirect(remove_query_arg($array, false));
    }
}

Also, you are right. The page will become slow with the current approach since it will load twice.

发布评论

评论列表(0)

  1. 暂无评论