1

I try to call a function everytime a user login to website.

I succeeded with the wp_login hook for user who fill the login form.

However I can't find a hook for an user which has the "remember me" option activated when he come back to website (so he already have a login cookie).

I checked wp_validate_auth_cookie() function but this one is fired on each page: is Wordpress doing a check for "remember me" cookie on each page and then set user logged?

Thanks

1

Don't use wp_login hook. Use wp_signon function instead.

https://developer.wordpress.org/reference/functions/wp_signon/

wp_signon( array $credentials = array(), string|bool $secure_cookie = '' )

The credentials is an array that has ‘user_login’, ‘user_password’, and ‘remember’ indices. If the credentials is not given, then the log in form will be assumed and used if set.

2
  • But wp_signon() function is only called when you submit login form. What about an user who come back to website another day (and is automatically logged by "remember me" option) ? – fdehanne Nov 6 '18 at 15:16
  • @fdehanne Then it is not clear what you're trying to do. There is no such thing as remember me hook. – sandrodz Nov 6 '18 at 16:57
1

wp_login hook pass two parameters: $user->user_login (string) and $user ( WP_User ). "remember me" option on login form did not pass on wp_login hook, its used on the wp_signon function to set the cookie on the browser.

wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);

So you can't access that using wp_login hook.

3
  • I know I can't access to it from wp_login hook, so I'm looking for another hook called for users with "remember me" option activated when they come back to website another day (and are automatically logged in). – fdehanne Nov 6 '18 at 15:23
  • There is no hook available to check remember post value, as its only used to set cookie on browser while login. – Maidul Nov 6 '18 at 15:45
  • I think there a misunderstanding : I don't want to get post value. When an user load a page how is he set logged to website by Wordpress when he already has this "remember me" cookie? (I've added more information on my question) – fdehanne Nov 6 '18 at 16:00
0

It's not 100% clear what you are trying to do, but in order to get the Remember Me status, you can do the following:

  1. Call wp_set_auth_cookie( $user_id, $remember ), where $remember is the Remember Me status.
  2. If you need to customize the actual session length based on the Remember Me setting, add a filter for the cookie expiration: add_filter( 'auth_cookie_expiration', 'get_expiration' ), 10, 3 ); with:
function get_expiration( $expiration, $user_id, $remember ) {
    // $remember will be passed from wp_set_auth_cookie()
    if ( $remember ) {
        // ...
    } else {
        // ...
    }
}
0

I know this is an old question but from what I understand, OP needs to call a custom function every time a user logs in (regardless of if it's via the login form or automatically logging in as a result of checking the Remember Me option during a prior login).
This can be achieved by using the 'authenticate' filter. More information can be found in this helpful guide that I found.

https://usersinsights.com/wordpress-user-login-hooks/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.