Completely Disable WordPress Search feature

A quick PHP code snippet to completely siege the default WordPress Search feature. Can be useful while implementing a third-party Search System.

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 to stop using WordPress search feature, and the best one for me was it throwing unrelated links in the site search results. However, I tweaked the search feature a bit and now I use it here as well as on my other sites.

Well, this post is not about why WordPress search is bad, its 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.

PHP code to siege WordPress Search

To disable the traditional WordPress search, just copy and paste the below code snippet into your theme’s functions.php file just before the closing PHP tag (?> in the end, and save the changes.

Note: Always create a backup of your theme before making any changes.

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;" ) );

That’s it! The search feature is now off on your WordPress installation and whenever someone tries to use hacks like “ random query” on your site, they will only get to see 404 pages.

You can remove the above code anytime if you change your mind. Hope this was helpful.