Opened 15 months ago
Closed 15 months ago
#60592 closed defect (bug) (worksforme)
Redefining roles
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Users | Keywords: | |
Focuses: | Cc: |
Description
My plugin code:
array_shift( wp_get_current_user()->roles );
Replaces all roles
print_r( wp_get_current_user() );
Result:
...
[roles] => Array
(
)
...
This affects other plugins that use this function call. Another example:
wp_get_current_user()->roles = [ 1, 2 ];
Function result:
...
[roles] => Array
(
)
...
Should it be like this?
Change History (1)
Note: See
TracTickets for help on using
tickets.
Hi there and welcome to WordPress Trac!
Yes. This is expected behavior in PHP and WordPress.
wp_get_current_user()
returns the global$current_user
object, which is always the sameWP_User
instance. So with your code you are modifying theroles
property on this object.You should make a copy of
wp_get_current_user()->roles
before making any modifications.So this is not a bug but working as intended.