Skip to content

Instantly share code, notes, and snippets.

@pradeepdotco
Created November 9, 2015 22:12
Show Gist options
  • Save pradeepdotco/646410736ee5f1d7c7f7 to your computer and use it in GitHub Desktop.
Save pradeepdotco/646410736ee5f1d7c7f7 to your computer and use it in GitHub Desktop.
Disable WordPress Search
function fb_filter_query( $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', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
@InstanceFactory
Copy link

InstanceFactory commented Jul 16, 2020

When I implelented this filter (WordPress 5.4.2), I had to make four changes:

line 5 changed to: $query->query_vars['s'] = false;
line 6 changed to: $query->query['s'] = false;

Note the ['s'] instead of [s]. Otherwise, I received the warning "Use of undefined constant s - assumed 's' (this will throw an Error in a future version of PHP)"

line 15 "create_function" is marked as depricated in 7.2, Instead, a anonymous (lambda-style) function should be created:

Also line 15, I had to change to return value to an empty string to hide the search. Returning null makes the default form appear, which is loaded / generated by general-template.php's function get_search_form (at least WordPress 5.4.2). The function applies the filter get_search_form. In case the filter returns null (null === $result, line 299), the previously loaded or generated default form will be shown or returned.

My line 15 now looks like this:

add_filter('get_search_form', function ($a) { return ''; });

My extended version (also unregisters the WP search widget) is available on GitHub at https://gist.github.com/InstanceFactory/73197b69681fa727e900f4f65086bbd1.

@JWPapi
Copy link

JWPapi commented Oct 15, 2021

This should include & is_admin() Otherwise it will break the searches in the admin area too

@KratosGemini
Copy link

I'm not sure what the purpose of if ( $error == true ) is. It seems to work just fine with $query->is_404 = true; being run unconditionally. Though @JWPapi is right. It needs && !is_admin() to avoid breaking page/post searches in admin.

On my site, I also added status_header( 404 ); to make it return a 404 status code on the page.

@adrearubin
Copy link

@JWPapi & @KratosGemini where did you place &is_admin? @InstanceFactory When i used your code above, and i type in my url www.blah.com/?s=test, i get a message on my page that says: "The page can’t be found. It looks like nothing was found at this location.". I need to use the ?s= parameter as a regular parameter, not a search. How can this be done? Thanks.

@JWPapi
Copy link

JWPapi commented Jun 16, 2022

function fb_filter_query( $query, $error = true ) {
	
	if ( is_search() & !is_admin()) {
		$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', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

@moonbyt3
Copy link

moonbyt3 commented Jul 8, 2022

create_function is depricated. and also query[s] is depricated.

How I solved this:

/*------------------------------------------------------------------------------------------
# Disables WordPress Search feature
------------------------------------------------------------------------------------------*/

function fb_filter_query( $query, $error = true ) {
	
	if ( is_search() & !is_admin()) {
		$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', 'fb_filter_query' );
add_filter( 'get_search_form', function($a) {return null;});

@InstanceFactory
Copy link

InstanceFactory commented Jul 9, 2022

... @InstanceFactory When i used your code above, and i type in my url www.blah.com/?s=test, i get a message on my page that says: "The page can’t be found. It looks like nothing was found at this location.". I need to use the ?s= parameter as a regular parameter, not a search. How can this be done? Thanks.

@adrearubin Well, I'm not that deep within WP. But I think you need to use a 'free' parameter, means, one that is not used by WP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment