Disabling default search in WordPress using PHP
On some WordPress sites—such as one‑page websites, landing pages, or sites with limited content—the default search function may not be useful. You can disable it using a simple PHP snippet. On ruachost.com, this is a safe way to remove the search feature without breaking your theme.
Why Disable Search?
Section titled “Why Disable Search?”- Prevents users from seeing irrelevant “No results found” pages.
- Simplifies navigation for one‑page or static sites.
- Reduces clutter in the WordPress dashboard and front‑end.
Steps to Disable Search via PHP
Section titled “Steps to Disable Search via PHP”-
Log in to WordPress with an administrator account.
-
In the dashboard, go to Appearance → Theme Editor.
-
Select your active theme and open the functions.php file.
-
Add the following code snippet at the end of the file:
// Disable WordPress default searchfunction disable_search($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', 'disable_search');add_filter('get_search_form', function($a) { return null; }); -
Click Update File to save changes.
-
Test your site → The search function will now be disabled.
Important Notes
Section titled “Important Notes”- Always back up your theme files before editing.
- Use a child theme to preserve changes during updates.
- This method disables both the search form and search queries.
- If you want to re‑enable search later, simply remove the code snippet.