wordpress create user from facebook login plugin

2019-06-09 10:10发布

How do I call this function from my wordpress theme. It is supposed to be called when the one who comments logs in via facebook login function.

function myfb_do_login() {
global $wpdb;

// cookie
$cookie = get_facebook_cookie();
// get user data
 $fbuser = get_facebook_user($cookie);
 $username = sanitize_user($fbuser->first_name);

// put everything in nice array
$userdata = array(
  'user_pass'       =>  wp_generate_password(),
  'user_login'  =>  $username,
  'user_nicename'   =>  $username,
  'user_email'  =>  $fbuser->email,
  'display_name'    =>  $fbuser->name,
  'nickname'        =>  $username,
  'first_name'  =>  $fbuser->first_name,
  'last_name'       =>  $fbuser->last_name,
  'role'            =>  'subscriber'
);

// create new user
$new_user = wp_insert_user($userdata);

// set the auth cookie to current user id
wp_set_auth_cookie($new_user, true);

// log the user in
wp_set_current_user($new_user);

// do redirect  here
wp_safe_redirect(get_permalink(). '#response');
}

1条回答
Fickle 薄情
2楼-- · 2019-06-09 10:22

if you have user info at hand, create an array of user data an pass it to wp_insert_user, what userdata should contains refers to wp_insert_usercodex.

code example:

//insert new user to db
$wpuid=wp_insert_user($userdata);
//set the auth cookie to current user id
wp_set_auth_cookie($wpuid,true);
//log the user in
wp_set_current_user($wpuid);
//do redirect  here....
wp_safe_redirect($location);
查看更多
登录 后发表回答