• After seeing, that a lot of people would need it and not finding any documentation or any code that works, here goes my sollution:

    $cur_user_roles = $current_user->roles;
            foreach($cur_user_roles as $cur_user_roles_value) {
                $role_to_compare = $cur_user_roles_value;
            }
    
            if($role_to_compare == 'administrator') {
                echo "Administrator ";
            } elseif($role_to_compare == 'editor') {
                echo "Editor ";
            } elseif($role_to_compare == 'author') {
                echo "Author ";
            } elseif($role_to_compare == 'contributor') {
                echo "Contributer ";
            } elseif($role_to_compare == 'subscriber') {
                echo "Subscriber ";
            } else {
                echo "<i>Att.: user got no actual role in the system</i> ";
            }

    It would even work, when a user would have got multiple roles (downgrading – highest is a match).

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Franz Josef Kaiser

    (@f-j-kaiser)

    BUT: I still got the problem, that this won`t work with a redirect based on the user_role. I added the following to my functions.php and it doesn′t work (the “else” case always is true):

    add_filter( 'login_redirect', 'redirect_login' );
        function redirect_login( $c ) {
        global $current_user;
        global $wpdb;
        get_currentuserinfo();
        $cur_user_roles = $current_user->roles;
            foreach($cur_user_roles as $cur_user_roles_value) {
                $role_to_compare = $cur_user_roles_value;
            }
                if($role_to_compare == 'administrator') {
                    return get_bloginfo( 'url' ) . '/list_aus';
                } elseif($role_to_compare == 'editor') {
                    return get_bloginfo( 'url' ) . '/editor';
                } elseif($role_to_compare == 'author') {
                    return get_bloginfo( 'url' ) . '/author';
                } elseif($role_to_compare == 'contributor') {
                    return get_bloginfo( 'url' ) . '/contributor';
                } elseif($role_to_compare == 'subscriber') {
                    return get_bloginfo( 'url' ) . '/subscriber';
                } else {
                    return get_bloginfo( 'url' ) . '/none';
                }
        }
    
        add_action( 'wp_logout', 'redirect_logout' );
        function redirect_logout() {
               wp_safe_redirect( get_bloginfo('url') );
               exit();
        }
    Thread Starter Franz Josef Kaiser

    (@f-j-kaiser)

    ok. now you can get it with for ex. current_user_can(‘administrator’).

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_User > which role has the logged in user’ is closed to new replies.