Disabling default search in WordPress using PHP
Learn how to disable the default search function on WordPress sites. This article shows you how to use a short PHP function to disable the default search function.
Having a search function on WordPress sites with limited ages or one-page navigation sites is not helpful for the users. This article shows you how to use a short PHP function to disable the default search function.
Disabling search in WordPress
Important
Always perform a backup before you make any changes to the theme files. If you break any codes, it will be easier to revert your site to its last good known state. Alternatively, you could also create a child theme. Read this link on how to create a child theme: https://www.hosting.com/blog/wordpress-child-theme/
Follow the steps below to edit your Theme setting file to manually disable search in WordPress:
-
Log in to your WordPress site with an administrator account.
-
On the Dashboard in the left sidebar, click Appearance, and then click Theme Editor:
-
On the Theme Editor, select the Theme you want to edit from the dropdown:
-
The files for this selected theme are listed on the right column under Theme Files. Click on the file named "functions.php":
function disable_search( $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', 'disable_search' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
- Insert the following code to the end of functions.php file and click Update File Button to save the changes:
Related Articles
Updated 1 day ago