WordPress.org

Ready to get started?Download WordPress

Forums

How to get the current logged in user's role? (10 posts)

  1. wp_ravi
    Member
    Posted 4 years ago #

    How to get the current logged in user's role?

  2. Adam Harley
    Member
    Posted 4 years ago #

    You can retrieve the role object for the current user by calling get_role(), but that's an object rather than a string with the role name. You'd probably be better off using current_user_can() which can check whether the current user is allowed to do something. current_user_can( 'manage_options' ) tends to be a good way of determining an admin.

  3. raskull
    Member
    Posted 4 years ago #

    ok... so instead of the standard "Edit" link appearing next to a post or comment when a user is logged in. I want conditional links to appear: "Admin: Edit", "Editor: Edit", "Author: Edit", "Contributor: Edit"... How to do?

  4. caugb
    Member
    Posted 4 years ago #

    I have a function to do what you want. It get the data from database, without using functions like get_currentuserinfo or get_userdata:

    function get_user_role($uid) {
    global $wpdb;
    $role = $wpdb->get_var("SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'wp_capabilities' AND user_id = {$uid}");
      if(!$role) return 'non-user';
    $rarr = unserialize($role);
    $roles = is_array($rarr) ? array_keys($rarr) : array('non-user');
    return $roles[0];
    }
  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    <?php
    if ( is_user_logged_in() ) {
    	$userRole = ($current_user->data->wp_capabilities);
    	$role = key($userRole);
    	unset($userRole);
    	$edit_anchr = '';
    	switch($role) {
    		case ('administrator'||'editor'||'contributor'||'author'):
    			$edit_anchr = ucfirst($role).': <a href="'.get_edit_post_link( $post->ID ).'">Edit</a>';
    		break;
    		default:
    		break;
    	}
    	echo $edit_anchr;
    }
    ?>

    Like that perhaps..... :)

    $current_user is a WordPress global.

  6. raskull
    Member
    Posted 4 years ago #

    This seems to work, but didn't quite get me there. Maybe it will help someone else though.

    if ( is_user_logged_in() ) {
    	$user = new WP_User( $user_ID );
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    		foreach ( $user->roles as $role )
    			echo $role;
    	}
    }

    thanks to
    http://wordpress.org/support/topic/275681?replies=7
    and
    http://codex.wordpress.org/Function_Reference/get_currentuserinfo

    Shouldn't this be easier?

  7. itissue
    Member
    Posted 3 years ago #

    Is there a way to get a user's role within a plugin so you can define who can access the plugin's options menu?

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

    You should just check against a capability instead..

    For example, if you wanted to ensure a particular portion of code only runs or is shown for admins.

    if( current_user_can( 'manage_options' ) ) {
    
    // Do something, for admins only
    
    }

    Menus in the admin section have a capability parameter when you register them (third parameter).
    http://codex.wordpress.org/Adding_Administration_Menus

  9. Lawrence
    Member
    Posted 3 years ago #

    hey guys,

    did a quick function that simply echo's the current users role to the screen. It was mainly to display a message saying "welcome administrator". Put the code below in your theme functions.php, or set up a plugin and you're sorted.

    function get_user_role() {
    	global $current_user;
    
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    
    	return $user_role;
    }

    then in your html theme you simply put <?php echo get_user_role(); ?>.

    As t31os_ mentioned though, you shouldnt really use this to compare roles... but there's no harm in displaying it.

  10. Simon Wheatley
    Code for the People
    Posted 3 years ago #

    Here's a version which will translate the role name too:

    /**
     * Returns the translated role of the current user. If that user has
     * no role for the current blog, it returns false.
     *
     * @return string The name of the current role
     **/
    function get_current_user_role() {
    	global $wp_roles;
    	$current_user = wp_get_current_user();
    	$roles = $current_user->roles;
    	$role = array_shift($roles);
    	return isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : false;
    }

    I'll second Grimbog and t31os_ in suggesting you only use this to show the user role on screen.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags