X

Completely Disable Public Search in WordPress

Sometimes you want to totally disable search functionality on your website. However, removing the search field doesn’t do the job as you’d still be able to modify the URL to search content. That’s why we made it so you can add the following snippet to the functions.php of your WordPress theme to totally disable public search.


Code Snippet:


function wpsnipp_disable_public_search( $q ) {
if ( $q->is_admin ) {
return $q;
}

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

add_action( 'parse_query', 'wpsnipp_disable_public_search', 5 );

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!