Removing Admin Bar Node Based on Role

WP_User has a roles array.

Get the current user with wp_get_current_user() and check if your role is in the array.


add_action( 'admin_bar_menu', 'remove_new_content_menu', PHP_INT_MAX );

function remove_new_content_menu( $wp_admin_bar ) {

    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the `new content` button
    $blacklisted_roles = array('grocery', 'subscriber');

    // remove the button if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        $wp_admin_bar->remove_node( 'new-content' );
    }
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)