Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Plugin update first_name, last_name in wp_usermeta on activation


peterverkooijen
Participant

@peterverkooijen

I had a good feeling about this attempt, but still nothing 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]}"];
}

$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);

This version messes up registration:

function synchro_wp_usermeta($user_id, $password, $meta) {
global $bp_user_signup_meta, $bp, $wpdb;

$fields = BP_XProfile_Field::get_signup_fields();

$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]}"];
}

$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);

Registration goes through as normal. The user gets one email with the activation link. But the page on the link says ‘Your account has already been activated. You can now log in with the account details that were emailed to you.’ No email arrives.

So the above code hijacks activation without doing anything (probably because $bp_user_signup_meta was added). Still nothing in wp_usermeta either.

My problem is getting the $fullname from $meta. I don’t understand the array stuff. I’m basically just guessing.

If there are any php coders out there that can spot any obvious mistakes or can suggest other things to try, please help me out.

Skip to toolbar