php - Wordpress ignore empty query paramaters

Let's say the URL is: https://example.com/?min_price=100&max_price
Themax_price
is set but empty. As a result, the product page will return empty.
I want to ignore all empty parameters, so it won't include in the query. Is there a hook, action, or filter for that?
Currently, my solution is by usingtemplate_redirect
andremove_query_arg
:
function your_redirection(){
if(isset($_GET['min_price']) || isset($_GET['max_price'])){
$array = array();
if(isset($_GET['min_price']) && (empty($_GET['min_price']) || !is_numeric($_GET['min_price']) || (is_numeric($_GET['min_price']) && $_GET['min_price'] < 1))){
$array[] = "min_price";
}
if(isset($_GET['max_price']) && (empty($_GET['max_price']) || !is_numeric($_GET['max_price']) || (is_numeric($_GET['max_price']) && $_GET['max_price'] < 1))){
$array[] = "max_price";
}
wp_redirect(remove_query_arg($array, false));
}
}
add_action( 'template_redirect', 'your_redirection' );
usingremove_query_arg
to remove the empty query and redirect. The problem is I don't think my approach is good enough, I can't even put anexit;
at the end ofwp_redirect();
function, otherwise, it won't work.
Answer
Solution:
Is client side allowed?
$(function(){
$("form").submit(function(){
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // continue submission
});
});
it will prevent the value of your form from being serialized and sent to the server.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.