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');
}
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_user
codex .
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);
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.