Completely Disable WordPress Search feature

2 minutes read

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.

  1. Head over to your WordPress dashboard
  2. In the Appearance section, navigate to Editor
  3. Look for the functions.php file there, and click it to edit
  4. 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;" ) );
  5. 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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

There are times when you need to disable comments on your WordPress site. There are two cases contextual to disabling WordPress comments: Disabling comments for individual posts and pages Disabling comments completely Disabling comments for individual WordPr...
Jetpack is a popular WordPress plugin developed by Automattic, the same company behind WordPress.com. It offers various features and tools to enhance the functionality and performance of a WordPress website. However, there might be instances where you want to ...
To disable WordPress email notifications, you can follow these steps:Login to your WordPress admin dashboard.Go to the "Settings" tab and click on "Discussion".Scroll down to the "Email me whenever" section.Uncheck the boxes for the not...