Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Plugin update first_name, last_name in wp_usermeta on activation


peterverkooijen
Participant

@peterverkooijen

You’ll need to have all three arguments, even if you don’t want to use them all. You can miss out ones AFTER the one you want, but you can’t miss anything out before the one you wish to use.

Are you sure about that? With the three arguments I get two ‘Missing argument’ error messages. With only $meta there are no errors – although it doesn’t do anything either…

EDIT: Tried the code below. Again one ‘Missing argument’ error. Registration goes through as normal, but nothing in wp_usermeta:

function synchro_wp_usermeta($user_id, $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 = $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' );

Skip to toolbar