Post profile Update function with [s2Member-Profile] fails to capture select fields

I’ve written a function to send my custom fields to an external API after a profile is updated from the [s2Member-Profile] page.

I get the user_id:
$user_info = get_userdata($user_id );

Then get the pofile fields I want:
$gender = get_user_field('gender', $user_id);

Then I send them along to my integration and log the changes.

Strangely, this fine for text fields, but not for selects. All my select fields return the value when the profile was loaded instead of showing the changed values.

I’ve tried using profile_update and ws_plugin__s2member_during_handle_profile_modifications and the result is identical for both (the fields themselves do update, so if I submit twice the correct value eventually gets through).

Thanks for any ideas!

Hi Craig.

Sorry for the late reply. I looked into this while working on the new release and found the timing issue you were running into.

WordPress’s profile_update hook fires before s2Member finishes saving its custom profile fields. The existing ws_plugin__s2member_during_handle_profile_modifications hook runs after those fields are saved, but before s2Member refreshes the user’s cached data, so re-reading a field with get_user_field() there can still give you the previous value.

I added a new hook in v260829 specifically for integrations that need to read the final saved profile data after that refresh:

ws_plugin__s2member_during_handle_profile_modifications_after_user_refresh

So your customization could do something like this:

add_action('ws_plugin__s2member_during_handle_profile_modifications_after_user_refresh', function($vars)
{
    $user_id = $vars['user_id'];

    $user_info = get_userdata($user_id);
    $gender    = get_user_field('gender', $user_id);

    // Send the freshly updated values to your integration here.
});

I specifically tested changing a select field through [s2Member-Profile] and reading it again with get_user_field() from this new hook, and it returned the newly saved value.

I hope that helps you! Please give that a try and let me know how it goes.

:slight_smile:

2 Likes

Thank you, this looks fantastic, I appreciate your taking the time to add that and respond!

1 Like