• Hi all-

    I’ve been trying to figure out how to login a WordPress user after validating their login via Facebook.

    I got the Facebook validation part working which sets the cookie and I can read the values fine but just can’t figure out how to login the user into WordPress without their password.

    I’ve dug through many Facebook WP plugins to try and find an answer but they all use the old api.

    Here’s how it works: User clicks the FB login button on the wp-login.php page. They login to FB and then the WordPress login page reloads and executes the code below.

    $fbuid = $facebook->getUser(); // gets the FB user id
    
    $wp_user_id = $wpdb->get_var("SELECT user_id FROM $wpdb->usermeta WHERE meta_value = '$fbuid' AND meta_key = 'facebook_uid' LIMIT 1" );
    
    if ($wp_user_id) {
        // must be an existing user so let's auto log them in
    
        $user = get_userdata($wp_user_id);
    
        require_once(ABSPATH . 'wp-blog-header.php');
        wp_set_current_user($wp_user_id);
        wp_set_auth_cookie($wp_user_id);
        do_action('wp_login', $user->user_login);
    
    } else {
    
        // no user found 
    
    }

    This part doesn’t work for some reason:

    `require_once(ABSPATH . ‘wp-blog-header.php’);
    wp_set_current_user($wp_user_id);
    wp_set_auth_cookie($wp_user_id);
    do_action(‘wp_login’, $user->user_login);`

    Does anyone have experience with the new Facebook open graph api or have the minimum code to automatically login a WP user with just their user id and/or username?

    Much appreciated!

    ~David

  • The topic ‘Login to WordPress with Facebook via open graph api’ is closed to new replies.