Your cart is currently empty!
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”
hello, i tried all but i cant remove serach bar
how can i remove search bar exactly
thank youCheck 404.php file there must be code for search function.
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
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;Im having the same problems with no help
If code doesn’t work for you any error message you see on website?
Grabber Pro
Original price was: $59.$39Current price is: $39.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19.Android App for Your Website
Original price was: $49.$35Current price is: $35.Abnomize Pro
Original price was: $30.$24Current price is: $24.Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Handle Sudden Traffic Spike in Website – Do Node Balancer Really Help
- How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
- How to Upload Images and PDFs in Android Apps Using Retrofit
- [Fix] File Permission Issue in Apache Website WordPress Not Writable to 775
- Best PHP ini Settings for WordPress & WooCommerce: Official Recommendations
Leave a Reply