php - Add first name and last name automatically on facebook registration for wordpress user -
i'm using code create facebook user when logging facebook connect button:
if($_request['option'] == "fblogin"){ global $wpdb; $appid = get_option('afo_fb_app_id'); $appsecret = get_option('afo_fb_app_secret'); $facebook = new facebook(array( 'appid' => $appid, 'secret' => $appsecret, 'cookie' => true, )); $fbuser = $facebook->getuser(); if ($fbuser) { try { $user_profile = $facebook->api('/me'); } catch (exception $e) { echo $e->getmessage(); exit(); } $user_fbid = $fbuser; $user_email = $user_profile["email"]; $user_fnmae = $user_profile["first_name"]; if( email_exists( $user_email )) { // user member $user = get_user_by('login', $user_email ); $user_id = $user->id; wp_set_auth_cookie( $user_id, true ); } else { // user guest $random_password = wp_generate_password( 10, false ); $user_id = wp_create_user( $user_email, $random_password, $user_email); wp_set_auth_cookie( $user_id, true ); } wp_redirect( site_url() ); exit; } }
the user created email name facebook profile missing. don't know how send wp_create_user
make it.
i tried no luck (added $user_fnmae):
} else { // user guest $random_password = wp_generate_password( 10, false ); $user_id = wp_create_user( $user_email, $random_password, $user_email, $user_fnmae ); wp_set_auth_cookie( $user_id, true ); }
any idea mistake?
wp_create_user() wordpress doc
try this:
$user_id = wp_create_user($user_fnmae , $random_password, $user_email);
take care mistype: user_fnmae => user_fname
edit:
if need set values (e.g. first_name, last_name, display_name, ...), use: wp_insert_user()
$userdata = array( 'user_login' => $user_email, 'user_pass' => $random_password, 'first_name' => $user_fnmae ); wp_insert_user($userdata);
Comments
Post a Comment