How to remove query string from current page URL?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x