1

I am looking to programmatically login users from php, but after reviewing multiple threads about achieving this I have found that none of the methods work for me and I am not sure why. Currently I am running the code from the functions.php file of my theme:

function test_login() {

    if (!isset($_GET['uname'])) return;

    function login($user) {
        do_action('wp_login', $user->data->user_login, $user);
        wp_set_current_user( $user->ID, $user->data->user_login );
        wp_set_auth_cookie( $user->ID, true );
        header('hdrs: are_setting');
    }

    $creds = array();
    $creds['user_login'] = $_GET['uname'];
    $creds['user_password'] =  'password';
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ) {
        // echo $user->get_error_message();
    } else {
        login($user);
        wp_redirect( 'https://example.com/home' );
        // exit;
    }
}

add_action('after_setup_theme', 'test_login'); // Also tried init

I have also tried running this code in a separate file, but there is no difference. The code finds the user no problem, the login function is executed and I can even see the hdrs header in my browser. But after the redirect I am still logged out. I do suspect that caching may be involved in this, but I tried disabling all my caching plugins, but there may be something going on with the hosting provider or cloudflare. Either way, a "manual" login works without issue. I hope that I am missing something obvious here.

1 Answer 1

0

This modified version of your code works perfectly for me:

function test_login() {
    if ( empty( $_GET['uname'] ) || is_user_logged_in() ) {
        return;
    }

    $user = wp_signon( [
        'user_login'    => sanitize_text_field( wp_unslash( $_GET['uname'] ) ),
        'user_password' => 'password',
        'remember'      => true,
    ] );

    if ( is_wp_error( $user ) ) {
        wp_die( $user->get_error_message() );
    } else {
        wp_clear_auth_cookie(); // clear any user cookies set in the browser
        wp_set_current_user( $user->ID, $user->user_login ); // set current site user
        wp_set_auth_cookie( $user->ID ); // set user cookie
        do_action( 'wp_login', $user->ID, $user ); // trigger core action.
    }
}
add_action( 'after_setup_theme', 'test_login' );
3
  • 1
    Your example does not work in my case, but I have found that it does work when the code runs on /wp-login.php page. Definitely something to do with Cloudflare then. But my code works there as well, so... I will wait a bit to see if someone has a solution that will work on any page, but otherwise I will just accept this answer. Commented Aug 21 at 14:51
  • Actually I had the same issue on a site I was working on where I created a custom login page (see here if curious). The way I managed to get it to work was to use the same parameters as WordPress for the username and passwords. So instead of using $_GET['uname'] use $_POST['log'] and for the password use $_POST['pwd'] - I didn't dig too deep into why it works, but it seems to. Cloudflare would have zero affect...
    – WPExplorer
    Commented Aug 27 at 2:10
  • Forgot to mention...Redirecting the user after login should fix things as well. The issue that I've found is that when they get logged in the cookie is added to the page but a refresh is necessary for them to actually become logged in.
    – WPExplorer
    Commented Aug 27 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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