Issue Dynamically Populating a Field Connected to the User Registration Feed [RESOLVED]

I have a custom function to add one year to a date stored in the usermeta table, and is populated dynamically into a field on the form. This works well until I connect the User Registration feed to update the usermeta. Once connected the form field gets populated with the data in the database and not my custom function.

To test this I duplicated the form field which is dynamically populated using the same parameter via my custom code in functions.php. Upon loading the form the first field shows the data stored in the usermeta table (incorrect) and the duplicated field, which in not set to the User Registration feed, shows the date plus one year (correct).

How can I get the custom code to populate the field connected to the User Registration feed, so it can be updated with the correct date? Below is my custom function for reference.

# Populate "Expiration Date" hidden field + 1 year for the membership renewal form
function create_new_renewal_date() {
  // If it's not a logged-in user viewing this, never mind
    if( ! is_user_logged_in() ) :
      return;
    endif;
  // Get the "_expire_user_date" user meta value for the current user
  $current_expire_date = get_user_meta( get_current_user_id(), '_expire_user_date', true );
  $current_date = strtotime("now");
  if ($current_expire_date > $current_date) { // check if member is expired
    $expire_date = date('Y-m-d H:i:s', $current_expire_date);
    $updated_renewal_date = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($expire_date)) ); // add 1 year to members exp. date
    $updated_renewal_date = strtotime($updated_renewal_date); // make it a timestamp again
    return $updated_renewal_date;
  } else {
    return strtotime('+1 year');
  }
}
add_action( 'gform_field_value_newexpiresdate', 'create_new_renewal_date' );

Thanks in advance for any help!

If you don’t map the user meta field to the form field, the form field will not be populated from the user’s meta. Will that help you achieve what you need?

No, as I need that date to be updated once the form is submitted.

You can use the approach I mentioned, and then use gform_after_submission hook to update the user data:

That way, the feed will not interfere with your dynamic population using gform_field_value.

Will that work?

Thanks, I’m almost there. However I’m having trouble getting the field value into the usermeta key field after the form submitted. Per my code…

# Update _expire_user_date after renewal form is updated
add_action("gform_after_submission_5", "mcgwd_update_expire_user_date"); // form id #5
function mcgwd_update_expire_user_date() {
  $expire_user_date_tbu = rgar($entry, '16'); //get entry of field id #16
  GFCommon::log_debug( __METHOD__ . "(): EUD is now: {$expire_user_date_tbu}" );
  update_user_meta( $user_id, '_expire_user_date', $expire_user_date_tbu );
}

… the debug log shows the variable ($expire_user_date_tbu) I’m retrieving from my form field as empty. Thus when the form is submitted the user meta field for ‘_expire_user_date’ is blank.

I assume I should be using something other than rgar to retrieve the field’s value?

It appears I now have it working. I’ll share my code below in case it may be helpful to those with a similar issue:

# Update _expire_user_date after renewal form is updated
add_action("gform_after_submission_5", "mcgwd_update_expire_user_date", 10, 2); // form id #5
function mcgwd_update_expire_user_date($entry, $form) {
  $expire_user_date_tbu = $entry['16']; //get entry of field id #16
  GFCommon::log_debug( __METHOD__ . "(): Expire date to be updated: {$expire_user_date_tbu}" );
  update_user_meta( get_current_user_id(), '_expire_user_date', $expire_user_date_tbu );
}
1 Like