Using wordpress login functions

Does anybody have any experience writing a custom WordPress login page using the functions:

wp_signon()
and wp_set_auth_cookie()

found on http://codex.wordpress.org/Function_Reference/

Read More

I can’t seem to get them working.

The code looks something like this:

function login_wordpress($username, $password) {
    $creds = array();
    $creds['user_login'] = $username;
    $creds['user_password'] = $password;
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ) {
       echo $user->get_error_message();
       die();
    } else {
         wp_set_auth_cookie( $user, 0, 0);
    }
}

Am I missing something basic?

Related posts

Leave a Reply

4 comments

  1. You need to change this line:

    wp_set_auth_cookie( $user, 0, 0);
    

    To this:

    wp_set_auth_cookie( $user->ID, 0, 0);
    

    $user is a WP_User Object not user id.

    wp_signon returns WP_Error on failure, or WP_User on success.

  2. Was having real trouble myself… finally got it to work..!!
    (after several days of experimenting and banging my head against it)

    One thing to make sure is that you haven’t sent any output yet
    or the session cookie won’t write as it needs to be in the header.
    Also if you call wp_signon before the session starts, there session
    info gets lost also… sheesh… strange but I had both happening
    to me. Anyhoo no more ado…

    // Create new user (for example)

    $userdata = array('user_login'->$username,'user_pass'->$password);
    $user_id = wp_insert_user($userdata);
    

    // Make sure username is up to date… I needed this as there was
    a hook to user_register being called in wp_insert_user but that
    hook is called after the user is created, so it needed a DB
    cache clear to work otherwise the wrong username was set…
    So the user was auto-logged in for one page only – ridiculous.
    (It was Register Plus Redux doing the hooking btw.)

    wp_cache_delete($user_id, 'users');
    wp_cache_delete($username, 'userlogins');
    $userdata = get_userdata($user_id);
    $username = $userdata->user_login;
    

    // Make sure user session has started

    $vsessionid = session_id();
    if (empty($vsessionid)) {session_name('PHPSESSID'); session_start();}
    

    // Login Current User

    wp_clear_auth_cookie();
    $creds = array();
    $creds['user_login'] = $username;
    $creds['user_password'] = $password;
    $creds['remember'] = true;
    $user = wp_signon($creds, false);
    

    // Check it worked

    if (is_wp_error($user)) {$error = $user->get_error_message();}
    else {
        wp_set_current_user($user_id);
        // The next line *really* seemed to help!
        do_action('set_current_user');
        $current_user = wp_get_current_user();
        if (is_wp_error($current_user)) {$error = $user->get_error_message();}
    }
    
    if ($error) {echo $error; print_r($userdata); print_r($current_user);}
    
  3. current_user();
    
    function current_user()
    {
        global $current_user,$user_ID;
    
        if(is_user_logged_in())
        {
            echo 'User Logged in '.$user_ID;
        }
        else {
            echo 'No user is logged in< br/>';
            custom_login();
        }
    }
    
    function custom_login() {
        $creds = array('user_login' => '<USERNAME>', 'user_password' =>   '<USERPASSWORD>', 'remember' => true );
        $user = wp_signon( $creds, false );
        wp_set_current_user($user->ID); 
        wp_set_auth_cookie($user->ID, true, false );    
        do_action( 'wp_login', '<USERNAME>' );
    
        if ( is_wp_error($user) )
            echo $user->get_error_message();
        }
    }
    
  4. I did this in a project a few years ago, so the WordPress code was a bit different. But this code worked for me:

    // include the wordpress files necessary to run its functions
    include('../classpages/wp-config.php'); // this includes wp-settings.php, which includes wp-db.php, which makes the database connection
    include(ABSPATH . WPINC . '/pluggable-functions.php');
    
    // use wordpress's function to create the login cookies
    // this creates a cookie for the username and another for the hashed password, which wordpress reauthenticates each time we call its wp_get_current_user() function
    wp_setcookie($user_login, $user_pass, false, '', '', $cookieuser);
    

    I didn’t have to use wp_signon at all, but that may have changed.

    Are you getting an error message, or what do you see when you run your code?