Re: Plugin update first_name, last_name in wp_usermeta on activation
This latest attempt had no error messages, but also no first_name and last_name were added to wp_usermeta:
function synchro_wp_usermeta($meta) {
global $bp, $wpdb;
$fullname = $meta['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' );
?>
I suspect I need some additional code to “unpack” $meta.
In a previous attempt I tried this, based on a function in bp-xprofile-signup.php:
// Extract signup meta fields to fill out profile
$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();
}
$fullname = $field->field_1;
I think it crashed my server…
I’ll need help (un?)serializing that $meta array. I’ll study up on that stuff later.
But first I’ll try if bp_user_fullname() works. After some late lunch…