Make WordPress Core

#60592 closed defect (bug) (worksforme)

Redefining roles

Reported by: n1ck500's profile n1ck500 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

(

[0] => 1
[1] => 2

)

...

Should it be like this?

Change History (1)

#1 @swissspidy
15 months ago

  • Component changed from Plugins to Users
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Severity changed from critical to normal
  • Status changed from new to closed

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 same WP_User instance. So with your code you are modifying the roles 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.

Note: See TracTickets for help on using tickets.