-
AuthorPosts
-
July 3, 2021 at 9:17 pm #1308598
Hi,
Because of the woocommerce, for the other users ( they are customers ) I disable admin bar. However, It disappears in some normal pages and appears again inconsistently and it is definitely missing in category or product pages when I login as an admin. Hard to edit and surf between pages as an admin like this. I use this code below. It is a well known code. What could be the reason? Any idea?
/* Disable the WordPress Admin Bar for all but admins. */ add_action('after_setup_theme', 'remove_admin_bar'); function remove_admin_bar() { if (!current_user_can('administrator') && !is_admin()) { show_admin_bar(false); } }
Thanks
- This topic was modified 3 years, 5 months ago by ilkbaharkunduzu.
July 4, 2021 at 11:25 pm #1308685Hey ilkbaharkunduzu,
I believe that !current_user_can is intended to check for capabilities not roles.
Try changing to (!current_user_can(‘edit_pages’) please see Roles and Capabilities.Best regards,
MikeJuly 5, 2021 at 6:28 am #1308701Hi,
I have the same exact copy of the site in local and actually I can see the bar as an administrator at top of all the pages. That is weird.
I zip the functio.phpn file in the child theme (for back up) then opened it in Sublime Text (via ftp) , changed like you suggested but could not see the bar. I deleted all cookies etc. Then tried. Nope. It is the same. Then I mistakenly added a comma or something in function and updated then the site gave a fatal error. :) Then I deleted the function file to be quick in Cyberduck and expand the .zip file I backed up. Now I can see the bar as an administrator with the original code. Nothing has changed, the same code I shared…
What is happening. :) Kidding me..Is that because of the cookies deleted…Or loaded functions.php file again I do not understand how…Any Idea?
By the way this is a so basic ( and a must) code and wordpress should have a predefined action for this woocommerce customers. Nobody wants to show that bar to the customers…
Best Regards
- This reply was modified 3 years, 5 months ago by ilkbaharkunduzu.
July 6, 2021 at 11:52 am #1308919Hi,
I found your code in this article and tested it on my site with an additional test user that has the role of “customer” and the function works correctly:add_action('after_setup_theme', 'remove_admin_bar'); function remove_admin_bar() { if (!current_user_can('administrator') && !is_admin()) { show_admin_bar(false); } }
This code checks if the current user is not an administrator, and they are not viewing the admin dashboard. If both conditions match, then it will disable the WordPress admin bar.
This function also works correctly:
add_filter( 'show_admin_bar', '__return_false' );
This code will disable the admin bar for all users when viewing the public pages of your website. All users will still be able to see the toolbar inside the WordPress admin dashboard.
The article also suggests a plugin: Hide Admin Bar Based on User Roles.
So please carefully check your functions.php code for errors and try again, I recommend using a Child ThemeBest regards,
Mike -
AuthorPosts
- You must be logged in to reply to this topic.