#18932 closed defect (bug) (fixed)
WP_User::set_role leaves user object without any caps when role does not change
| Reported by: | jammitch | Owned by: | ryan |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.5 |
| Component: | Role/Capability | Version: | 3.1 |
| Severity: | minor | Keywords: | has-patch |
| Cc: | Focuses: |
Description
The first thing WP_User::set_role does is clears out the user's existing caps array. Then it checks to see if the passed-in role is the user's current role, and quits the function if so. This is great if the role actually changes. If it does not, this leaves the user with the same role, but without any caps whatsoever for the remainder of the current page.
Core WordPress code may not use the function in this way, but plugins may.
Workaround:
Plugins calling set_role should wrap the call in the same-role check found inside the function.
Fix:
Do the same-role check prior to unsetting the caps.
Attachments (1)
Change History (8)
Note:
See TracTickets
for help on using tickets.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Confirmed:
add_action( 'admin_notices', function() { $user = new WP_User( 2 ); // 2 being the id of a user with the 'subscriber' role print_r( $user->caps ); $user->set_role( 'subscriber' ); print_r( $user->caps ); });