Remove Search Function for Logged in/out Users WordPress

Categories:

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.

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.

Comments

6 responses to “Remove Search Function for Logged in/out Users WordPress”

  1. atm.fatma Avatar
    atm.fatma

    hello, i tried all but i cant remove serach bar

    http://prntscr.com/q5ehi2

    how can i remove search bar exactly
    thank you

    1. Sandy Avatar

      Check 404.php file there must be code for search function.

  2. Steve Margolin Avatar
    Steve Margolin

    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

  3. Steve Avatar
    Steve

    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;

  4. Quamaine Whitlow Avatar
    Quamaine Whitlow

    Im having the same problems with no help

    1. Sandy Avatar

      If code doesn’t work for you any error message you see on website?

Leave a Reply

Your email address will not be published. Required fields are marked *