Данный код срабатывает при сохранении изменений в профиле пользователя WordPress.
Синхронизирует поля из usermeta в x-profile
function xprofile_sync_bp_profile2( &$errors, $update, &$user ) {
// Bail if profile syncing is disabled.
if ( bp_disable_profile_sync() || ! $update || $errors->get_error_codes() ) {
return;
}
// Querying data from a table "usermeta", first we get the value of the custom field "address" and substitute this value for the "Address" field of the xprofile BuddyPress
$address = get_user_meta( $user->ID, 'address', 'true' );
xprofile_set_field_data( 'Address', $user->ID, $address );
//Similarly with other fields
$firstname = get_user_meta( $user->ID, 'first_name', 'true' );
xprofile_set_field_data( 'Name', $user->ID, $firstname );
$lastname = get_user_meta( $user->ID, 'last_name', 'true' );
xprofile_set_field_data( 'Lastname', $user->ID, $lastname );
$description = get_user_meta( $user->ID, 'description', 'true' );
xprofile_set_field_data( 'Bio', $user->ID, $description );
// Querying data from a table "user"
xprofile_set_field_data( 'E-mail', $user->ID, $user->user_email );
xprofile_set_field_data( 'Site', $user->ID, $user->user_url );
}
add_action( 'user_profile_update_errors', 'xprofile_sync_bp_profile2', 10, 3 );
Точно так же можно осуществлять и обратную синхронизацию:
// 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' );
Источник второго варианта:
https://buddypress.org/support/topic/how-to-use-custom-name-fields-with-xprofile/