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!
2
Answers
This is an alternative answer. I know my original question was asking about the ID number, but after a lot of experimentation, I learned that there's no need for this code at all (which addresses the second part of my question, "am I approaching this issue wrong?").
Gravity forms actually has the in-built ability to write data to custom fields (even for users through a registration process), but does not have the ability to create custom fields itself. I finally found this information in the Gravity Forms Documentation.
Doing so is very simple, but you have to have the PRO version. To do this:
Gravity Forms handles the rest.
So while Fredrik's answer is correct, if you are able to create a custom field, you can just use Gravity Forms existing functionality and avoid adding extra code altogether.
Hope this helps someone as the gravity_post_submission hook is... finicky at best - at least in my experience.
Try something like this.