Tagged: Admin Bar, conditional, Frontend, user role
After looking at this thread (https://buddydev.com/support/forums/topic/disable-admin-bar-for-all-users-except-for-administrators-code-not-working/)
I was wondering…
How can we create a snippet to have a conditional admin bar in frontend upon user status. I explain.* Non logged user = WP Admin bar not displayed at all (personnaly I don’t put a login access here, but I made a special login page with better security)
* logged user = WP Admin bar with profile and activity stream)
* bbpress moderator = + forum menu
* admin = normal barFollowing these tips I roughly understand the concept, but not able to do it.
https://www.sitepoint.com/customize-wordpress-toolbar/ and can’t figure out how to distinguish simple logged from moderator or admin.If you can tell me how to do it right, I will be even more grateful to you.
Thx
BrunoHi Bruno,
Thank you fro the question.I believe that the tutorial provides fairly good amount of details.
you can combine it with some capability check to get the desired result.
https://developer.wordpress.org/reference/functions/current_user_can/
https://wordpress.org/support/article/roles-and-capabilities/
Regards
BrajeshHi Brajesh,
Ok, I’m not a PHP guy (I’m a graphist) anyway.
Here is the code I created. Is there an easier way to do this?‘// Update Admin Toolbar Function
function my_update_adminbar($wp_adminbar) {// remove unnecessary items
$wp_adminbar->remove_node(‘wp-logo’);
$wp_adminbar->remove_node(‘bp-login’);
$wp_adminbar->remove_node(‘new-content’);
$wp_adminbar->remove_node(‘site-name’);
$wp_adminbar->remove_node(‘search’);
}
function my_update_adminbar_modo($wp_adminbar) {// remove unnecessary items
$wp_adminbar->remove_node(‘wp-logo’);
$wp_adminbar->remove_node(‘bp-login’);
$wp_adminbar->remove_node(‘site-name’);
$wp_adminbar->remove_node(‘new-media’);
}// admin_bar_menu hook action if not admin
if (! is_user_logged_in()) {
add_filter(‘show_admin_bar’, ‘__return_false’);}
elseif ( is_user_logged_in() && ! current_user_can( ‘publish_forums’ )) {
add_action(‘admin_bar_menu’, ‘my_update_adminbar’, 999);}
elseif ( is_user_logged_in() && current_user_can( ‘publish_forums’ )) {
add_action(‘admin_bar_menu’, ‘my_update_adminbar_modo’, 999);}
elseif ( current_user_can( ‘manage_options’ ) ) {
add_filter(‘show_admin_bar’, ‘__return_true’);
}’
Thx
You must be logged in to reply to this topic.