WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 11 months ago

#11635 closed defect (bug) (fixed)

create a function that does this: wp_get_user_role()

Reported by: hakre Owned by: ryan
Milestone: 3.6 Priority: normal
Severity: normal Version: 2.9
Component: Role/Capability Keywords: has-patch close
Focuses: Cc:

Description

There is an TODO task left in the file wp-admin/user-edit.php around line 196. The original author should decide wether or not he wants to implement the noted feature or to just drop it.

Related Code:

// Get the highest/primary role for this user
// TODO: create a function that does this: wp_get_user_role()
$user_roles = $profileuser->roles;
$user_role = array_shift($user_roles);

Attachments (3)

11635.diff (1.4 KB) - added by ericmann 4 years ago.
Adds wp_get_user_role() function that keys off $user_id
wp-get-user-role.diff (2.7 KB) - added by wonderboymusic 21 months ago.
11635.2.diff (1.7 KB) - added by wonderboymusic 16 months ago.

Download all attachments as: .zip

Change History (20)

comment:1 hakre4 years ago

  • Owner set to ryan
  • Status changed from new to assigned

code was comitted by ryan.

comment:2 hakre4 years ago

in [10319] use wp_dropdown_roles(). Props jeremyclarke. fixes #8764

comment:3 scribu4 years ago

wp_get_user_role() would be really handy. +1

ericmann4 years ago

Adds wp_get_user_role() function that keys off $user_id

comment:4 ericmann4 years ago

  • Cc eric@… added
  • Keywords has_patch needs_testing added

comment:5 scribu4 years ago

  • Keywords dev-feedback has-patch needs-testing added; developer-feedback has_patch needs_testing removed

comment:6 scribu4 years ago

Does the patch work for if is_user_logged_in() is false?

comment:7 ericmann4 years ago

The patch works when is_user_logged_in() is false at least on my installation. I think it could warrant further tests, though, in case I missed something.

comment:8 dd324 years ago

  • Component changed from General to Role/Capability
  • Milestone changed from 3.0 to 3.1

Users can have multiple roles. There is no real way to determine which role is the "highest".

I'm proposing we leave this for 3.1, There are a few people who are wanting to clean roles up in 3.1

comment:9 nacin3 years ago

  • Milestone changed from Awaiting Triage to Future Release

comment:10 bainternet2 years ago

  • Cc admin@… added

comment:11 wonderboymusic21 months ago

I'll see your $user_id and raise you a $user object and then default to $current_user - also implemented the function in more places

comment:12 scribu21 months ago

We should really get wp_get_user() in: #21120

comment:13 scribu21 months ago

But other than that, I agree with dd32:

Users can have multiple roles. There is no real way to determine which role is the "highest".

comment:14 wonderboymusic21 months ago

Semantics - return "first" role - the 3 or 4 places it is used were already arbitrarily returning the first role. I know multiple roles per user per blog exists, but I wish it didn't. Joining user_meta to subdivide users completely breaks at scale (over 1M users, many millions of rows of meta)

wonderboymusic16 months ago

comment:15 follow-up: wonderboymusic16 months ago

  • Keywords close added; dev-feedback needs-testing removed
  • Milestone changed from Future Release to 3.6

I think the need for this is obsolete. I refreshed the function to return the first role or a passed role if the user has it. But it's only returning the label for the role ('administrator' etc). So the function only returns the first role or does the equiv of a current_user_can - the TODO has been there since the beginning of time: http://core.trac.wordpress.org/browser/trunk/wp-admin/user-edit.php?rev=10935#L251 . Moving to 3.6 so it gets used or closed.

comment:16 in reply to: ↑ 15 DrewAPicture12 months ago

I agree with @wonderboymusic and @dd32. At the least, just remove the TODO, but since users can have multiple roles, this doesn't necessarily return the "highest" role, just the first one. +1 for close.

comment:17 ryan11 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 24247:

Remove obsolete TODO.

Props wonderboymusic
fixes #11635

Note: See TracTickets for help on using tickets.