Make 2+ part name in full name required + xprofile_sync_wp_profile()
-
The cleanest way to get separate firstname and lastname into the database seems to be to
1. make a two or more part name in fullname on the registration form required
2. consistently split the input from Buddypress’ fullname field and synchronize with usermeta using xprofile_sync_wp_profile()
How would I do that? I assume this could be turned into a plugin?:
function xprofile_sync_wp_profile() {
global $bp, $wpdb;
if ( (int)get_site_option( 'bp-disable-profile-sync' ) )
return true;
$fullname = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );
$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( 'xprofile_updated_profile', 'xprofile_sync_wp_profile' );Hook it to register_post? Does this code already check if there’s a two part name? I suspect that happens at ‘if ( false === $space)…etc.’, but I don’t see an error message + redirect or something like that.
Or is it impossible to use this code during register_post because of this line:
$bp->loggedin_user->id
How can I modify the code to make it work during registration? Can I just remove that line? Or run this function the first time the new user logs in? Is there a hook on_activation or something like that?
Has anyone used xprofile_sync_wp_profile()? Can anyone share code examples? This would solve a major structural problem in Buddypress imho.
I need to come up with a solution, because integration with most mailing lists depends on having firstname and lastname stored somewhere.
Alternatively I can use fullname for firstname and create a second custom xprofile field for lastname, but I suspect that solution could cause lots of problems further down the line.
- The topic ‘Make 2+ part name in full name required + xprofile_sync_wp_profile()’ is closed to new replies.