Instant download after payment Secure checkout Contact support

WordPress How to Tutorials

Remove Search Function for Logged in/out Users WordPress

23 July 2026

You can restrict search feature in WordPress for based on users type like Logged-in and Logged-out users control what they see or for all visitors as well.

There are 2 way to do this first you can disable search widget to be appear for logged-in users and other to disable search function for logged-out users in this way registered and non registered website users see different things.

disbale-search-function

Hiding Search Bar for Register or non register users in WordPress

It's depend on what function your theme is calling most of them uses get_search_form to make condition for this. Open your file and place below code.

if ( is_user_logged_in() ) {
   get_search_form();
}

if you want to display something else to logged-out users

if ( is_user_logged_in() ) {
   get_search_form();
}
else {
   echo "Hello Users.";
}

Disabling Search Function

If you want to completely disable search function for logged out users that they can't ever search query URL like "/?s=" than use below code in your function.php file

if ( !is_user_logged_in() ) {
	function insertcart_filter_query( $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', 'insertcart_filter_query' );
}

Remove Search widget for non website users

To remove search widgets use below function in function.php file

if ( !is_user_logged_in() ) {
add_filter( 'get_search_form', create_function( '$hidesrc', "return null;" ) );
function remove_search_widget() {
    unregister_widget('WP_Widget_Search');
}
add_action( 'widgets_init', 'remove_search_widget' );

 }

If you have any question please post in comment.

6 comments

  • atm.fatma 3 December 2019
    hello, i tried all but i cant remove serach bar http://prntscr.com/q5ehi2 how can i remove search bar exactly thank you
  • Sandy 3 December 2019
    Check 404.php file there must be code for search function.
  • Steve Margolin 31 January 2021
    Hi, I just found your PHP code for disabling the search function in Wordpress...just what I needed. But when I try to search BEFORE logging in, I see this error on the 404 page: Warning: Use of undefined constant s - assumed 's' (this will throw an Error in a future version of PHP) in /home/customer/www/villasup.com/public_html/wp-content/themes/zakra-child/functions.php on line 36 FYI, line 36 is this: $query->query_vars[s] = false; My website is https://villasup.com/?s=villas Is there any way to prevent this error? Thanks. Steve M
  • Steve 31 January 2021
    I think I solved it by adding ' marks around variable s. So the line: $query->query_vars[s] = false; would become: $query->query_vars['s'] = false;
  • Quamaine Whitlow 27 August 2021
    Im having the same problems with no help
  • Sandy 9 September 2021
    If code doesn't work for you any error message you see on website?