How to disable search feature in WordPress

The Search feature of WordPress is around for a long time. However, if you do not want your users to search through your site for some reason, then this snippet will be helpful.

It not just removes the search bar in your sidebar menu but removes the entire concept of WP search. This can be helpful when your site does not contain any content to be searched.

Just add this snippet to your function.php file.

function wps_search_query( $query, $error = true ) {

    if ( is_search() ) {

         $query->is_search = false;
         $query->query_vars[s] = false;
         $query->query[s] = false;

         // to error
         if ( $error == true ){
              $query->is_404 = true;
         }

    }

}

add_action( 'parse_query', 'wps_search_query' );

add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

In this function firstly we are checking the query is for search. then with the $query variable, we are setting it to false.

Now with the $error, we are checking if it is true or not. If true then we are setting $query to 404 error. At last, we are adding this function with the help of add_action.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top