How to disable the WordPress search functionality

Add this snippet to your (child-)themes functions.php to disable the search widget and redirect any search query to 404 or any other URL:

function tk_filter_query( $query, $error = true ) {
  if ( is_search() ) {
    $query->is_search = false;
    $query->query_vars[s] = false;
    $query->query[s] = false;
    if ( $error == true )
  $query->is_404 = true;
  }
}
add_action( 'parse_query', 'tk_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
function tk_remove_search_widget() {
  unregister_widget('WP_Widget_Search');
}
add_action( 'widgets_init', 'tk_remove_search_widget' );

 

Leave a Comment

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