Sunday, April 15, 2012

Remove search from WordPress

Here's what you need to do to disable search in WordPress.

This will hide the search box in your template and also disable the handling of search requests (e.g. when a visitor or a search engine makes a direct request to the search script), therefore the search functionality will be removed completely.

Open the functions.php file from your template folder (located in /wp-content/themes/) and put the following code into it:

function my_parse_query($q) {
if($q->is_search()) {
unset($_GET['s']);
unset($_POST['s']);
unset($_REQUEST['s']);
$q->is_search = false;
$q->set('s', '');
$q->set_404();
}
}

add_action('parse_query', 'my_parse_query');

function my_search_form($form) {
return '';
}

add_filter('get_search_form', 'my_search_form');


The first function will be called during the query handling. It will clear the variables that contain the search query. The 404 error page will be shown as well. The second function will be called during the search box generation in your theme. It returns an empty value, therefore your theme will show nothing instead of the regular search form.

If your theme contains a file called searchform.php (that is a template file which also contains the search form), then it has to be renamed or deleted.

Source:
Thanks to coffee2code for the ideas. That website provides a WP plugin for this, but I prefer adding a few lines of code to my template instead of using the plugin.

No comments:

Post a Comment