0
votes

Let's say the URL is: https://example.com/?min_price=100&max_price

The max_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 using template_redirect and remove_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' );

using remove_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 an exit; at the end of wp_redirect(); function, otherwise, it won't work.

1

1 Answers

0
votes

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.