Update a S2Member profile field on registration

I need to update a user field with custom information when they register. It is a user registration number that has the year, month, and user ID.

I can do this easily with a regular custom field, using update_usermeta, however I can’t figure out how to do this with an S2member custom field.

Here’s what I have:

add_action( 'user_register', 'assign_member_number');
    function assign_member_number($user_id) {

      $auto_membership_number = date('y'). date('m') . $user_id;
      update_usermeta( $user_id, 'membership_number', $auto_membership_number );

    }

The S2Members custom field that I want to populate is also called membership_number. How can I populate this field automatically like above?

I tired: update_usermeta( $user_id, ‘s2member_membership_number’, $auto_membership_number );

But it does not work.

Thanks!

You’re using a deprecated function. Take a look here at the one that’s replaced it: https://developer.wordpress.org/reference/functions/update_user_meta/

If that doesn’t work, you might need to access the database directly. See this: http://wordpress.stackexchange.com/questions/39010/update-user-option-not-working-as-expected

Cool, okay I got it working. The trick was update_user_options. I found it here..

The code looks like this:

add_action( ‘user_register’, ‘assignuserid’);

function assignuserid($user_id) {

$auto_membership_number = date(‘y’). date(‘m’) . $user_id;

// update_user_meta( $user_id, ‘membership_number’, $auto_membership_number );

$custom_fields = get_user_option(‘s2member_custom_fields’, $user_id);

$custom_fields[‘membership_number’] = $auto_membership_number;

update_user_option($user_id, ‘s2member_custom_fields’, $custom_fields);

}