admin管理员组

文章数量:1289875

I am trying to update a BuddyBoss Profile when a users wordpress profile is updated in the user edit page from the backend/admin. (Basically I want to sync the data). I have this function:

function BB_WP_SYNC_update_BB_profile( $user_id ) {
    
        // Get user information    
     $country = get_user_meta($user_id, 'ofc_country', true);
     error_log($country);
     $region = get_user_meta($user_id, 'ofc_regional_organisation', true);
     error_log($region);
     $gender = get_user_meta($user_id, 'ofc_gender', true);
     error_log($gender);
     $dob = get_user_meta($user_id, 'ofc_date_of_birth', true);
     $timestamp = strtotime(str_replace('/', '-', $dob));

     $date = date('Y-m-d H:i:s', $timestamp);

     //Update BuddyBoss Profile
     xprofile_set_field_data('Country', $user_id, $country, true);
     xprofile_set_field_data('Regional Organisation', $user_id, $region, true);
     xprofile_set_field_data('Gender', $user_id, $gender, true);
     xprofile_set_field_data('Date of Birth', $user_id, $date, true);

}
add_action( 'edit_user_profile_update', 'BB_WP_SYNC_update_BB_profile');
add_action( 'personal_options_update', 'BB_WP_SYNC_update_BB_profile');

The user information this function gets is the old information, prior to it being updated. For example, if I change the Country from Australia to Tahiti, then $country in my function is set to Australia.

I have to click the update profile button a second time for it to get the new value (which has obviously been set from the first click), and then it updates BuddyBoss correctly.

It seems that 'edit_user_profile_update' & 'personal_options_update' hook in before the user meta fields are updated.

Is there something else I can use to ensure both user meta fields & user fields are updated before the hook fires?

I am trying to update a BuddyBoss Profile when a users wordpress profile is updated in the user edit page from the backend/admin. (Basically I want to sync the data). I have this function:

function BB_WP_SYNC_update_BB_profile( $user_id ) {
    
        // Get user information    
     $country = get_user_meta($user_id, 'ofc_country', true);
     error_log($country);
     $region = get_user_meta($user_id, 'ofc_regional_organisation', true);
     error_log($region);
     $gender = get_user_meta($user_id, 'ofc_gender', true);
     error_log($gender);
     $dob = get_user_meta($user_id, 'ofc_date_of_birth', true);
     $timestamp = strtotime(str_replace('/', '-', $dob));

     $date = date('Y-m-d H:i:s', $timestamp);

     //Update BuddyBoss Profile
     xprofile_set_field_data('Country', $user_id, $country, true);
     xprofile_set_field_data('Regional Organisation', $user_id, $region, true);
     xprofile_set_field_data('Gender', $user_id, $gender, true);
     xprofile_set_field_data('Date of Birth', $user_id, $date, true);

}
add_action( 'edit_user_profile_update', 'BB_WP_SYNC_update_BB_profile');
add_action( 'personal_options_update', 'BB_WP_SYNC_update_BB_profile');

The user information this function gets is the old information, prior to it being updated. For example, if I change the Country from Australia to Tahiti, then $country in my function is set to Australia.

I have to click the update profile button a second time for it to get the new value (which has obviously been set from the first click), and then it updates BuddyBoss correctly.

It seems that 'edit_user_profile_update' & 'personal_options_update' hook in before the user meta fields are updated.

Is there something else I can use to ensure both user meta fields & user fields are updated before the hook fires?

Share Improve this question asked Jun 30, 2021 at 4:48 roly151roly151 354 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

For anyone else with this issue, the fix was simple. I changed the hook and most importantly, I changed the priority so my action comes later. Default wordpress priority was 10, so I set priority to 11 and it works.

add_action( 'profile_update', 'BB_WP_SYNC_update_BB_profile', 11, 2);

And my function changed due to the different hook:

function BB_WP_SYNC_update_BB_profile( $user_id, $old_user_data ) {

本文标签: hooksHow to get user meta fields that have just been updated