Skip to content

Instantly share code, notes, and snippets.

@AndreFCAmorim
Created November 6, 2022 15:38
Show Gist options
  • Save AndreFCAmorim/460b88f91a01ac2e475e18c9d6528680 to your computer and use it in GitHub Desktop.
Save AndreFCAmorim/460b88f91a01ac2e475e18c9d6528680 to your computer and use it in GitHub Desktop.
Disable Search on WordPress
<?php
// Prevent search queries.
add_action(
'parse_query',
function ( $query, $error = true ) {
if ( is_search() && ! is_admin() ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
if ( true === $error ) {
$query->is_404 = true;
}
}
},
15,
2
);
// Remove the Search Widget.
add_action(
'widgets_init',
function () {
unregister_widget( 'WP_Widget_Search' );
}
);
// Remove the search form.
add_filter( 'get_search_form', '__return_empty_string', 999 );
// Remove the core search block.
add_action(
'init',
function () {
if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
return;
}
$block = 'core/search';
if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
unregister_block_type( $block );
}
}
);
// Remove admin bar menu search box.
add_action(
'admin_bar_menu',
function ( $wp_admin_bar ) {
$wp_admin_bar->remove_menu( 'search' );
},
11
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment