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.
1 Answer
Reset to default 0You 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.
remove_query_arg
look pretty clear? – mozboz Commented Jul 28, 2020 at 10:09