Last active
June 21, 2022 14:27
-
-
Save techb/9370e6a13ec6641228ccab0a06017399 to your computer and use it in GitHub Desktop.
Disable WordPress search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Yoink: https://www.wpbeginner.com/wp-tutorials/how-to-disable-the-search-feature-in-wordpress/ | |
// Tested on WordPress: 5.9.3 | |
// PHP: 8.0 | |
/* | |
Instead of going to a search page, show 404 instead | |
*/ | |
function wpb_filter_query( $query, $error = true ) { | |
if ( is_search() ) { | |
$query->is_search = false; | |
$query->query_vars['s'] = false; | |
$query->query['s'] = false; | |
if ( $error == true ) | |
$query->is_404 = true; | |
} | |
} | |
add_action( 'parse_query', 'wpb_filter_query' ); | |
/* | |
This removes the search widget from the UI. | |
But only for PHP 7.x and below, this will NOT work for PHP 8.x | |
*/ | |
// add_filter( 'get_search_form', create_function( '$a', "return null;" ) ); | |
// function remove_search_widget() { | |
// unregister_widget('WP_Widget_Search'); | |
// } | |
// add_action( 'widgets_init', 'remove_search_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment