The default WordPress search is the only thing that used to annoy me a lot and I wasn’t using it on any of my WordPress sites. There are a number of reasons back then to stop using WordPress search feature, and the best one for me was it throwing unrelated links in the site search results.
However, we can tweak the traditional search feature a bit to make it better and keep using it on our sites.
Well, this post is not about why WordPress search is bad, it’s about how to forbid or disable WordPress Search for others. This can be helpful if you are planning to use a third-party site search solution like Google Custom Search etc.
Using a Plugin to disable the search
It is as simple as doing nothing. Install and activate Disable Search WordPress plugin, and that’s it! You just said goodbye to WordPress search.
PHP code to siege WordPress Search
For code loves, here is some copy-paste code delight that goes straight in your theme’s functions.php
file. Here are some steps for beginner-level reader.
Note: Always create a backup of your theme before making any changes.
- Head over to your WordPress dashboard
- In the Appearance section, navigate to Editor
- Look for the
functions.php
file there, and click it to edit - Now, copy the below PHP code, and paste just at the end of everything, or before that closing PHP tag (
?>
, if any)function disable_search( $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', 'disable_search' ); add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
- Click “Save” to save the changes
Done! The search feature is now off on your WordPress installation and whenever someone tries to use hacks like “http://wpcrux.com/?s=some random query” on your site, they will only get to see a 404 page, as usually displayed by the 404.php
file in WordPress.
You can remove the above code anytime if you change your mind. Hope this was helpful.