Re: Plugin update first_name, last_name in wp_usermeta on activation
Stuck again…
I think this is the function that takes the input from the registration form (in bp_xprofile_classes.php):
function get_signup_fields() {
global $wpdb, $bp;
$sql = $wpdb->prepare( "SELECT f.id FROM {$bp->profile->table_name_fields} AS f, {$bp->profile->table_name_groups} AS g WHERE g.name = %s AND f.parent_id = 0 AND g.id = f.group_id ORDER BY f.id", get_site_option('bp-xprofile-base-group-name') );
if ( !$temp_fields = $wpdb->get_results($sql) )
return false;
for ( $i = 0; $i < count($temp_fields); $i++ ) {
$fields[] = new BP_XProfile_Field( $temp_fields[$i]->id, null, false );
}
return $fields;
}
Then bp-xprofile-signup.php has the function xprofile_load_signup_meta() with this bit:
$fields = BP_XProfile_Field::get_signup_fields();
if ( $fields ) {
foreach ( $fields as $field ) {
$value = $_POST['field_' . $field->id];
Followed by lots of validation code. For my purposes I could end it here:
$fullname = $_POST['field_1'];
Trying to put it together:
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( 'user_register', 'synchro_wp_usermeta' );
Does this make sense? Can any of the more experienced php-coders please point out the obvious mistakes?