WordPress Snippet: Disable search

I have a very small WordPress blog with a very cheap hosting. I do not wish to allow my users to do search on my WordPress blog to help conserve some of the resources. What should I do? – Mildred

You can use the below WordPress code snippet to disable WordPress search.

NOTE: To insert the snippet into your WordPress site, we recommend using the Code Snippets plugin instead of modifying your functions.php file.

// Snippet from NibbleGuru.com
function disable_search_action($query, $error = true) {

    // Let's first check if this is a WordPress search
    if (is_search()) {

        // Unset search related variables
        $query->is_search = false;
        $query->query_vars[s] = false;
        $query->query[s] = false;
 
        // Set error as 404 (Page Not Found)
        if ($error == true) {
            $query->is_404 = true;
        }
    }
}

// Hook into parse_query if someone types the search url directly
// or click a direct link to search results to disable search
// and show 404 error.
add_action('parse_query', 'disable_search_action');

// Replace the search form to be empty so it will not be shown.
add_filter('get_search_form', create_function('$a', "return null;"));

Be the first to comment

Leave a Reply