Re: Plugin update first_name, last_name in wp_usermeta on activation
This version also doesn’t work. No errors, registration goes through as normal, but still no first_name and last_name in wp_usermeta:
function synchro_wp_usermeta($user_id, $password, $meta) {
global $bp, $wpdb;
$field_ids = $meta['xprofile_field_ids'];
$field_ids = explode( ',', $field_ids );
// Loop through each bit of profile data and save it to profile.
for ( $i = 0; $i < count($field_ids); $i++ ) {
if ( empty( $field_ids[$i] ) ) continue;
$field_value = $meta["field_{$field_ids[$i]}"];
$field = new BP_XProfile_ProfileData();
$field->user_id = $user_id;
$field->value = $field_value;
$field->field_id = $field_ids[$i];
$field->last_updated = time();
$field->save();
}
$fullname = $field_value['field_1'];
$space = strpos( $fullname, ' ' );
if ( false === $space ) {
$firstname = $fullname;
$lastname = '';
} else {
$firstname = substr( $fullname, 0, $space );
$lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
}
update_usermeta( $bp->loggedin_user->id, 'nickname', $fullname );
update_usermeta( $bp->loggedin_user->id, 'first_name', $firstname );
update_usermeta( $bp->loggedin_user->id, 'last_name', $lastname );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $bp->loggedin_user->id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $bp->loggedin_user->id ), $bp->loggedin_user->id ) );
}
add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);
Again, any suggestions that can move this along are very much appreciated.
The problem really is how to extract the fullname data from the meta field in wp_signups. Can I do something similar to this:
xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );
Is there a wp_signups_get_field_data I can tap into?
Or some type of database query?:
$wpdb->read( $wpdb->signups, array('???' => 1), array('??' => $key) );