Search code examples
wordpressgravity-forms-plugin

Retrieving Values From Gravity Forms for Custom User Meta


I'm a little frustrated with Gravity Forms at the moment. Here's my setup: I have a custom registration form created with WPEverest's User Registration plugin. This plugin creates a custom user registration form that seamlessly integrates with WooCommerce, and also saves custom fields to the USER, rather than in a separate table.

I'm trying to use Gravity Forms to allow users to update field's created with WPEverest's Registration Plugin(which at this point is just some custom user meta fields), but I'm having a hell of a time getting the field values.

My biggest issue is that I can't for the life of me figure out what the heck value I'm supposed to pass through get_post_custom_values();.

The code below is what I have so far. This is supposed to pass a value to the user meta field upon form submit, and it does work... but only if I set the value as a manually created variable. It will not work if I try to pull the data from Gravity Forms.

$gravity_form_id = '10'; // Gravity Forms ID
add_action("gform_after_submission_$gravity_form_id", "gravity_post_submission", 10, 2);
function gravity_post_submission ($entry, $form){

    $post_id = $entry["post_id"]; 
    
    //trying to get values from gravity form
    $values = get_post_custom_values("field_name???", $post_id);

    //updating user meta
    update_post_meta($current_user->ID, 'user_registration_body_type', $values);

}

For reference, the form object I'm using is a RADIO type with ID 10, and a parameter name of body_type (I'm using this name as a means to pre-populate the form fields).

Here's how the form field renders:

<div class="ginput_container ginput_container_radio">
  <ul class="gfield_radio" id="input_14_10">
    <li class="gchoice_14_10_0">
      <input name="input_10" type="radio" value="apple" id="choice_14_10_0">
      <label for="choice_14_10_0" id="label_14_10_0">Apple</label>
    </li>
    <li class="gchoice_14_10_1">
      <input name="input_10" type="radio" value="full-bust" id="choice_14_10_1">
      <label for="choice_14_10_1" id="label_14_10_1">Full Bust</label>
    </li>
    <li class="gchoice_14_10_2">
      <input name="input_10" type="radio" value="hourglass" checked="checked" id="choice_14_10_2">
      <label for="choice_14_10_2" id="label_14_10_2">Hourglass</label>
    </li>
    <li class="gchoice_14_10_3">
      <input name="input_10" type="radio" value="pear" id="choice_14_10_3">
      <label for="choice_14_10_3" id="label_14_10_3">Pear</label>
    </li>
    <li class="gchoice_14_10_4">
      <input name="input_10" type="radio" value="straight" id="choice_14_10_4">
     <label for="choice_14_10_4" id="label_14_10_4">Straight</label>
    </li>
  </ul>
</div>

TL;DR - What am I supposed to replace form_field??? with in order to retrieve the form field value? What ID or Name am I supposed to use? (ex: input_10, input_14_10, body_type ?) I'll be trying to do the same thing with checkboxes later, but it seemed easier to start with a radio button since it only returns one value.

Or am I approaching this problem all wrong?

Thanks in advance for your help!


Solution

  • Try something like this.

    • Add form id to the gform_after_submission_. It looks like you are using form id 14 but this code are for form id 10.
    • use rgar to specify the field id (10)
    • use update_user_meta
    • Get the current user ID (This means that you must be logged in before subitting the form.
    add_action("gform_after_submission_10", "gravity_post_submission", 10, 2);
    function gravity_post_submission ($entry, $form){
        
        //Gets field id 10
        $values = rgar( $entry, '10' );
        
        //updating user meta
        update_user_meta( get_current_user_id(), 'user_registration_body_type', $values );
        
    }