Re: Plugin update first_name, last_name in wp_usermeta on activation
You mean the 3 at the end of that line would fix that? I’ll try that next with a previous version of my code.
Just for the record, this attempt again didn’t work. No errors, but nothing in wp_usermeta either:
function synchro_wp_usermeta() {
global $bp_user_signup_meta, $bp, $wpdb;
$fields = BP_XProfile_Field::get_signup_fields();
if ( $fields ) {
foreach ( $fields as $field ) {
$value = $_POST['field_' . $field->id];
}
}
$fullname = $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_new_user', 'synchro_wp_usermeta' );
This fixes the missing arguments errors, but still nothing in wp_usermeta. I’ll need to figure out how to serialize (?) $meta and extract $fullname from it:
function synchro_wp_usermeta($user_id, $password, $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', 10, 3);