简体   繁体   中英

Facebook Login with WordPress

I that this questions has been asked in various forms, but none seem to address actually creating a WordPress User Account via the Facebook API.

I'm trying to clearly understand how one might use Facebook Login on a WordPress site, but I don't see how a User could be created without a password ... which Facebook won't expose, obviously. And I'm not looking for a plugin.

Perhaps a WordPress User is not created ... but I don't see how this is useful, other that superficial interactions like commenting on blog posts.

How can I create a WordPress User using Facebook Login?

My assumption is that I'll need to generate a password for the user, which they can change at any time in the future.

Usually, with the OAuth, at the end you will receive some info from the provider like email, username, id, or whatever. It varies per provider. You'll have to check what FB returns at the end of the process. Basically, the idea here is to search for user in your database at this step by FB ID (which you can store as user meta). If user is found, you simply log them in like this

wp_set_current_user($wp_user_id);
if (wp_validate_auth_cookie() == FALSE)
{
    wp_set_auth_cookie($wp_user_id, true, false);
}

If user is not found, you create a new user for them with

$userid = wp_insert_user(array('user_login' => $user_login,
    'user_email' => $user_email,
    //whatever other fields you need
    'display_name' => $user_name,
    'user_pass' => wp_generate_password(),
    'role' => 'subscriber',
));
update_user_meta($userid, '_fb_id', $fb_id);

wp_set_current_user($userid);
if (wp_validate_auth_cookie() == FALSE)
{
    wp_set_auth_cookie($userid, true, false);
}

You just generate random password for them. You don't need to know it and they don't need it either because it will never be inputted. You will simply set current user to them. Let me know if you have questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM