Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Disable search in WordPress without a plugin

Switch off front-end search with one Snipfire snippet. It covers the main work of the Disable Search plugin; a few small extras are different.

What Disable Search does

Disable Search stops the front end of your site handling searches and removes search forms, widgets and blocks. The plugin on WordPress.org

What the snippet does the same

  • Search requests show your theme's not found page with a 404 status.
  • Search forms print nothing, the Search widget is removed and Search blocks show nothing.
  • Search in the admin keeps working.

What's different

  • The plugin removes the search box from the admin bar on the front end. With the snippet, logged-in users still see it, but searches lead to the not found page.
  • The plugin removes the Search block from the block inserter. With the snippet, the block can still be added but shows nothing.
  • The plugin also removes the search action from Yoast SEO's structured data. The snippet doesn't.

When to keep the plugin Keep the plugin if you use Yoast SEO and want its search markup gone, or want the admin bar search box removed.

The snippet: Turn off site search

Search results show the “not found” page, and search forms and Search blocks print nothing.

PHP
<?php

add_action(
	'template_redirect',
	static function () {
		global $wp_query;
		if ( is_search() && ! is_admin() ) {
			$wp_query->set_404();
			status_header( 404 );
			nocache_headers();
		}
	},
	1
);
add_filter( 'get_search_form', '__return_empty_string', 99 );
add_filter( 'render_block_core/search', '__return_empty_string' );
add_action(
	'widgets_init',
	static function () {
		unregister_widget( 'WP_Widget_Search' );
	}
);

Notes, where it runs and what it was tested with

Switching from the plugin

  1. Install the snippet in Snipfire (it arrives switched off).
  2. Deactivate Disable Search.
  3. Switch the snippet on. Snipfire checks the code and test-loads your site first.
  4. Check the page it affects, then delete the plugin.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.