0

I am writing a custom frontend logon page for a theme I am working on. The logon page uses wordpress functions as much as possible (eg., wp_signon, wp_set_auth_cookie, ...etc.).

For logging on, I do the following

// Detect if we should remember the user
$remember = false;
if (isset($_POST['remember_me'])) {
    $remember = true;
}

// Sign in to WordPress
$creds = array(
    'user_login'    => $userlogin,
    'user_password' => $password,
    'remember'      => $remember
);

$logged_on_user = wp_signon($creds, false);

Note that if remember is set to true, this would allow WordPress to remember the logged on user (i.e., no need to log in again when a user closes the window). This remember field is based on a choice made by the user when logging in by clicking on a "remember me" checkbox.

At one point in my code, I like to detect if the user has set the rememeber field to true. I am not sure how can I detect the authorization cookie and read this value. I could not find documentation about that either. So, how can I do that? Thanks.

Edit

As per Dan's request in the comments below, here is what the login html looks like

<form method="post">

    <input type="hidden" name="form_name" value="login">
    <input type="hidden" name="nonce_key" value="<?php echo wp_create_nonce('nonce-key'); ?>" />

    <label for="user_login">Email or Username</label>
    <input type="text" name="user_login" value="<?php echo (isset($_POST['user_login']) ? htmlspecialchars($_POST['user_login']) : ''); ?>" />

    <label for="user_password">Password</label>
    <input type="password" name="user_password" value="" />

    <input type="checkbox" name="remember_me" <?php echo (isset($_POST['remember_me']) ? 'checked' : ''); ?> />
    <label for="remember_me">Remember me</label>

    <a href="<?php echo $reset_password_page_url; ?>">Forgot your password?</a>

    <input type="submit" name="submit" value="Log In" />

</form>
  • How does your HTML login form look like? – MinhTri Apr 20 '16 at 19:47
2

You can not and probably should not.

The proper full name of that setting should have been "remember me on this browser (for 14 days)". The point is that it is not even a user based settings and it can have different values on the same computer with different browser sessions.

In addition since it is fully user controlled you should not rely on it to have anything to do with what the user actually sent in the form at login time.

Of course if you have a use case in which the problems mentioned above do not apply, you can just set additional cookie that specifies that the user selected "remember me"

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.