Opened 2 years ago
Closed 2 years 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_userobject, which is always the sameWP_Userinstance. So with your code you are modifying therolesproperty on this object.You should make a copy of
wp_get_current_user()->rolesbefore making any modifications.So this is not a bug but working as intended.