WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 2 years ago

#28116 closed defect (bug) (fixed)

is_user_logged_in() does not work properly after using wp_signon()

Reported by: guusifs Owned by: DrewAPicture
Milestone: 4.9 Priority: normal
Severity: normal Version: 3.9
Component: Users Keywords: needs-docs
Focuses: Cc:
PR Number:

Description

I encountered is_user_logged_in() does not work properly after signing a user is logged in. Work around is described here: http://blog.rhysgoodwin.com/programming/wordpress-wp_signon-current_user-is-not-populated/. Guess that could also be the (quick) fix to just call wp_set_current_user at the end of wp_signon().

Change History (9)

#1 follow-up: @Denis-de-Bernardy
5 years ago

Seems invalid though: is_user_logged_in() returns false until the init hook, so if you run your code when the plugin is loaded then it will not work indeed.

#2 @chriscct7
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Invalid per comment:1

Last edited 4 years ago by chriscct7 (previous) (diff)

#3 @guusifs
4 years ago

Why is that? It took me a long time to figure out what was wrong.

#4 in reply to: ↑ 1 @SergeyBiryukov
4 years ago

Replying to Denis-de-Bernardy:

Seems invalid though: is_user_logged_in() returns false until the init hook, so if you run your code when the plugin is loaded then it will not work indeed.

I don't think that was the issue here, I think the report was about wp_signon() not setting the current user.

Replying to guusifs:

Why is that? It took me a long time to figure out what was wrong.

wp_signon() is only meant to check the credentials and set the authentication cookies. If you need to check is_user_logged_in() right away, wp_set_current_user() should be called explicitly.

Perhaps we need better docs, but as far as I can tell, there is no bug here.

Last edited 4 years ago by SergeyBiryukov (previous) (diff)

#5 @guusifs
4 years ago

Yes, that is what I was thinking, document better if this is how it works. How is that normally being done?

#6 @SergeyBiryukov
4 years ago

  • Keywords needs-patch needs-docs added
  • Milestone set to Awaiting Review
  • Resolution invalid deleted
  • Status changed from closed to reopened

Let's add a note to wp_signon() description that it does not set the current user.

#7 @ocean90
4 years ago

See #35488 for wp_logout().

#8 @DrewAPicture
2 years ago

  • Keywords needs-patch removed
  • Milestone changed from Awaiting Review to 4.9

#9 @DrewAPicture
2 years ago

  • Owner set to DrewAPicture
  • Resolution set to fixed
  • Status changed from reopened to closed

In 40943:

Docs: Add a note to the DocBlock for wp_signon() mentioning that it doesn't handle setting the current user.

Also outlines the conditions that must be met for is_user_logged_in() to work with it.

Part props SergeyBiryukov.
Fixes #28116.

Note: See TracTickets for help on using tickets.