WordPress comes bundled with tons of built-in features one of which is Search. As the name suggests it lets you search through the contents of your WordPress website. However, if for some reason you want to disable this feature you can easily do that using a tiny code snippet.
All you need to do is copy the following code snippet and paste it inside functions.php file located inside the root of your active theme folder.
- function csc_disable_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', 'csc_disable_filter_query' );
- add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
The above code snippet will redirect any search query performed by the user to a 404 page.
Also, you can place following code snippet into your functions.php file to disable Search widget from your WordPress website.
- function csc_disable_search_widget() {
- unregister_widget('WP_Widget_Search');
- }
- add_action( 'widgets_init', 'csc_disable_search_widget' );