admin管理员组文章数量:1331849
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.
本文标签: hooksHow to remove query string from current page URL
版权声明:本文标题:hooks - How to remove query string from current page URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742217103a2434770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
remove_query_arg
look pretty clear? – mozboz Commented Jul 28, 2020 at 10:09