so very odd…
if (isset($user->roles) && is_array($user->roles)) {
write_log_('user has roles'); }
the above is working I think.
WHY in the world WP does this… I have no idea, but:
1) upon loading wp-login.php, it fires login_redirect ** that is just wrong. SHOULD ONLY fire AFTER not before**
2) after clicking submit on login page, the user ID is not sent, BUT the hook does have user roles and cap. So it knows a user has successfully logged in but doesn’t have the user ID.
3) if by chance, you didn’t redirect based on #2, and you refresh the login page, the login_redirect NOW has the user ID.
that was very painful learning the above.
🙁
Does anyone disagree with the above?
thanks for the input!
The filter fires when ever the login page is loaded so that the $redirect_to variable is set. Whether the variable, and your filtered value in the process, is used depends on the login context. The value is not used until after the user logs in.
I believe the passed $user object not having the right ID is essentially due to a race condition. By the time your callback executes, WP has not yet completely established all user data.
I’m sorry your learning experience was painful. I suspect you will not forget it though 😀
thanks bcworkz,
🙂
from your other post to mine (thanks for that link!)
I see this:
https://codex.ww.wp.xz.cn/Plugin_API/Action_Reference/wp_login
IMO, that seems the most optimal place to redirect a logged-in user?
am I wrong? However, hardly anyone promotes that?
But
a) it only happens once after a successful login
b) you will “know” the user ID
as A and B above are NOT known in the other ‘related’ hooks about redirecting???
thoughts?
PS. What I don’t understand about WP Core developers and the documentation, this post/reply seems the MOST obvious info that ANY person working with user management in WP would need to know… but it basically does not exist in any simple format?
Am I missing something?
thanks for the help!
Heh, I just suggested wp_login in the other topic before seeing this thread 🙂
If you want a more comprehensive hook, may “authenticate” hooked with a large priority arg? By that time your callback should be passed either a WP_User object or a WP_Error object.
If not that, peruse through the source for wp_signon() and the other functions it calls to find alternative hooks you might use.
To keep further discussion in one place, I’m closing this topic. Please continue at https://ww.wp.xz.cn/support/topic/what-hook-should-be-used-to-restrict-page-access-based-on-user/