Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@InstanceFactory 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.

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