Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#46019 closed defect (bug) (invalid)

Themes using get_users() appear to change the current user

Reported by: lordmatt's profile lordmatt Owned by:
Milestone: Priority: normal
Severity: trivial Version: 5.0.3
Component: Users Keywords:
Focuses: Cc:


I have observed get_users() used to iterate through users (to make a list of current contributors) also changes the current user which causes the wrong user to be shown in the WP toolbar.

Change History (4)

#1 @swissspidy
5 years ago

  • Keywords reporter-feedback added
  • Severity changed from major to normal

Hi and welcome to Trac!

As far as I can see, neither get_users() nor WP_User_Query touch the $current_user global which is used by wp_get_current_user() or any other related globals, for that matter. Which means there shouldn't be such side effects.

Does this still happen when running code in one of the default themes with no plugins active? Can you perhaps share the code you're using?

#2 @lordmatt
5 years ago

  • Severity changed from normal to minor

You are right. I had second thoughts after I filed this ticket. I went back to the theme where I last saw the issue. It is used on so I assumed the code was solid.

I had created a watch array which checked the current user ID at the start and end of the template. It was different. By adding that a few more times I was able to see that get_users() is, as you say, innocent. The change happened after foreach ( $all_users as $current_user ) {...}

I did not find any documentation on it but I suspect that maybe $current_user is a global? Which is odd as $current_user is the variable used in code examples. Changing this var throughout the theme to $my_current_user removed the bug. I have left a support request for the theme author(s) so they can apply my changes.

Should $current_user be a global - or at least something that can be changed within the loop?

#3 @lordmatt
5 years ago

  • Severity changed from minor to trivial

Did some digging. Back c.2012 or before $current_user was a global (and still is for backwards compatibility). The true bug here, then is maybe that the WP toolbar depends on it? Is it worth posting a separate ticket for that? I don't want to overstep my bounds here.

#4 @swissspidy
5 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Yes, $current_user is a global variable in WordPress, but it is by no means deprecated.

Note: See TracTickets for help on using tickets.