How to disable empty search requests

Currently WordPress doesn’t include an option to disable the empty search queries, however, you can disable the ability to submit an empty search fields in WordPress by adding this code snippet into Theme options > Layout > Advanced > Custom JS (in footer):

$('input[type="search"').attr('required', 'required');

And if you want to customize WordPress to disable any empty search request and redirect them to the homepage you can add this code snippet into functions.php file inside your child theme:

function search_redirect($query) {
    if ( !is_admin() && $query->is_main_query() ){
        if ($query->is_search) { wp_redirect( home_url() ); exit; }
    }
}
add_action('pre_get_posts','search_redirect');
pixfort
pixfort
Essentials support team
hub.pixfort.com

Hey, this is pixfort support team, if you still need help you can always reach us via the chat messenger in your account on pixfort hub

Related Posts