Opened 2 years ago
Closed 2 years ago
#60592 closed defect (bug) (worksforme)
Redefining roles
| Reported by: | n1ck500 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Users | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.