Viewing 9 replies - 1 through 9 (of 9 total)
  • 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.

    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?

    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];
    }
    <?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.

    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
    https://www.ads-software.com/support/topic/275681?replies=7
    and
    https://codex.www.ads-software.com/Function_Reference/get_currentuserinfo

    Shouldn’t this be easier?

    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?

    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).
    https://codex.www.ads-software.com/Adding_Administration_Menus

    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.

    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to get the current logged in user’s role?’ is closed to new replies.