How to use custom Name fields with xprofile
-
Hi, I’ve found a simple way to change that Name field and the sync back to whatever you want it to be.
1. Make sure you have buddypress with extended profiles installed.
2. Create 3 field in the base section of Xprofile
For demo purpose we will name them :First name
Last name
Nickname
*(you can name them differently but make sure you update the function accordingly)3. Add that code to the function.php of your child theme.
// modified Xprofile sync. function xprofile_sync_wp_profile2( $user_id = 0 ) { // Bail if profile syncing is disabled if ( bp_disable_profile_sync() ) { return true; } if ( empty( $user_id ) ) { $user_id = bp_loggedin_user_id(); } if ( empty( $user_id ) ) { return false; } // get first name $firstname = xprofile_get_field_data('First name',$user_id ); // get last name $lastname = xprofile_get_field_data('Last name',$user_id ); // get nickname $nickname = xprofile_get_field_data('Nickname',$user_id ); // create fullname $fullname = trim( $firstname . ' ' . $lastname ); bp_update_user_meta( $user_id, 'nickname', $nickname ); bp_update_user_meta( $user_id, 'first_name', $firstname ); bp_update_user_meta( $user_id, 'last_name', $lastname ); global $wpdb; //you can modify that line to change the displayed name to something else than the nickname $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $nickname, $user_id ) ); } add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile2' ); add_action( 'bp_core_signup_user', 'xprofile_sync_wp_profile2' ); add_action( 'bp_core_activated_user', 'xprofile_sync_wp_profile2' );
4. This script also change the display name to the nickname but you can modify it to whatever you want. just replace $nickname in the indicated line by $firstname, $lastname or $fullname
That’s it
- The topic ‘How to use custom Name fields with xprofile’ is closed to new replies.