I am writing a plugin that fetches some extended user info from a remote service and I need it to execute its function each time a user logs in.
Is there a hook that gets fired after login that I can add an action to?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The action hook wp_login runs when the user logs in – it can run a simple function.
function do_anything() {
//do stuff
}
add_action('wp_login', 'do_anything');
documentation : https://codex.wordpress.org/Plugin_API/Action_Reference/wp_login
The real breadwinner here is wp_authenticate which has a bit of documentation. It passes an array with the given username and password, which gives you the opportunity to pass info to the remote service, if necessary.
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_authenticate
and to change the redirect URL after login, there is the filter login_redirect: https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
Method 2
I would caution against using wp_login. It is deprecated and in later versions of WordPress it may not work at all. Instead try the wp_signon function.
Edit: The wp_login function is deprecated but the wp_login action is still fine to use.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0