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!
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 );
}
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:
Create a Custom Field for the post type (or in the example's case, the user meta) - you can use something like Advanced Custom Fields for this, or custom code in your functions.php file
Use the Post Custom Field form item from the Advanced form item options in the form builder
From the Post Custom Field's Custom Field Name section, select "Existing" and then the name of the field you created from the dropdown.
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.
Related
When a buyer goes through the checkout process as anonymous guest, he specifies his email at the "Order information" step in the input
<input data-drupal-selector="edit-contact-information-email" type="email" id="edit-contact-information-email" name="contact_information[email]" value="" size="60" maxlength="254" class="form-email required" required="required" aria-required="true">
The email is stored in the column mail of the table commerce_order of the database.
I would like to store a text string via this input in the database.
If I fill the input by JS with text string and even change the type attribute of the input from email to text, the data is not stored in the DB. So there is email validating somewhere.
Where is the validating in JS or PHP?
Or how can I resolve the issue?
You should check the hook form_alter.
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/9
Also, you should take a look into the drupal checkout docs too:
https://docs.drupalcommerce.org/commerce2/user-guide/checkout/configure-checkout
You should be aware that changing a form type may impact all other places that needs to read this data.
Regarding the hook alter, Here goes an example.
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'myformID') {
$form['myformMachineName']['myfield']['#type'] = 'textfield';
}
}
I'm using Contact Form 7 in WordPress and I have many forms.
I'd like to add the form title (the post_title field in database terms) to all my existing forms, as a hidden field.
I'd like to use a hook so that I don't need to use a shortcode in each form in the admin area.
Any ideas? Thanks.
you could use CF7's 'wpcf7_form_hidden_fields' filter before your form is displayed,
add_filter('wpcf7_form_hidden_fields', 'add_form_title');
function add_job_title($hidden){
$form = wpcf7_get_current_contact_form();
$post = get_post($form->id());
$hidden['cf7_title'] = $post->post_title; //form title slug.
return $hidden;
}
when the form is submitted, $_POST['cf7_title'] will have the value of the hidden field.
I have a $_SESSION array variable with post ids. Inside foreach loop, I would like to get the posts titles of these ids. Thus so far I have something like this:
sport_title = '';
foreach($_SESSION['sports_post_id'] as $sports_id {
$sport_title = get_the_title($sport_id);
$sports_titles .= $sport_title . "<br />";
}
Now, my problem is that I do not know how to pass it in a custom variable in Contact Form 7 - Dynamic Text Extension plugin.
I have inside my form this field (inside CF7):
[dynamichidden dynamic_sports readonly default:shortcode_attr]
and inside my custom page template php file:
echo do_shortcode('[contact-form-7 id="3561" "CF7_get_custom_field dynamic_sports=\'$sports_titles\'" title="Availability Form EN"]');
Thus, I would like to send these post titles in email.. How can I make it work? thanks in advance
ok I figure it out how to do it! If anyone wants more explanation:
Inside Contact Form 7 - Form tab, I have insert this code:
[dynamichidden dynamic_sports "CF7_GET key='sports_post_id'"]
where key is a standard word (could not change it).
Inside Email tab, you should have this code:
Sports: [dynamic_sports]
Now, inside my Custom template PHP file, I have this shortcode:
echo do_shortcode('[contact-form-7 id="3561" title="Availability Form EN"]');
I also have a form with a hidden input type, with a name sports_post_id and value the id of the current post:
<input type="hidden" value="<?php echo get_the_title( get_the_ID() ); ?>" name="sports_post_id" id="sports_post_id" />
EDITED
Another solution via plugin that extends the CF7, would be the following:
Install Contact Form 7 - Dynamic Text Extension
Copy and paste the form-tag code below and then add it inside the form code block
[dynamichidden page-title "CF7_get_post_var key='title'"]
The above code will add a hidden text input to the form which will pre-populate the page title. This is good to use when you are using the same contact form on multiple pages so you know where the user has submitted the form from. Alternatively, you can display the page URL or slug using one of the below shortcodes instead:
[dynamichidden page-url "CF7_bloginfo show='url'"]
[dynamichidden page-slug "CF7_bloginfo show='url'"]
Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7
Finally, display the hidden dynamic content tag variable in Contact Form 7 form. While you are on the CF7 settings page, click on the "Email" tab and insert this:
[page-title]
If you are using the URL or Slug fields, you these instead:
[page-url]
[page-slug]
In your CF7 Form configuration > Email Tab, you only have to add the desired field between hooks [...]
[dynamic_sports]
This will print the dynamic field value in your email.
I’m trying to use the contact form in my template using do_shortcode and popup (Request availability button on a page). But I’ve noticed that form generated only inputs without <form> tag as usual so it failed to submit.
That’s how I’m implementing it in functions.php
function wc_shop_popup() {
$reqform = '[contact-form-7 id="987" title="Request availability"]';
echo '<a class="button button_full_width button_left req_button popup-link" href="#popup-availability" rel="lightbox" data-type="inline"><span class="button_icon"><i class="icon-layout"></i></span><span class="button_label">Request Availability</span></a>
<div id="popup-availability" class="popup-content mfp-hide"><div class="popup-inner" style="padding:20px;">'.do_shortcode($reqform).'</div></div>';
}
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_popup' );
This is the page - Request availability button. Any suggestions?
The problem was that contact form has been generated inside of add to cart form. I used another woocommerce hook called "woocommerce_after_single_product_summary" instead of "woocommerce_after_add_to_cart_button" and it's working!
I've been trying to figure out how to populate input fields in contact forms on my Wordpress website. I've tried using a plugin called Contact Form 7 dynamic text extension, and tried a pretty simple IF statement with PHP. But i just can't seem to get this to work.
With the plugin, i can only get half of the arguments to work in the shortcode, even then the form doesn't properly validate.
Using PHP as a placeholder="" outputs the code instead of the contents of the variable. for example "$current_user->user_firstname"
Any help on the matter would be much appreciated.
You can install this plugin
Contact Form 7 Dynamic Text Extension
Dynamic text and dynamic hidden fields are available using it.
And you can use the following code to grab the current user info into your field.
[dynamictext dynamicname "CF7_get_current_user"]
Or as mentioned in the answer above:
[dynamictext your-email "CF7_get_current_user key='user_email' "]
Note also that you can use dynamichidden for hidden fields like the following
[dynamichidden your-email "CF7_get_current_user key='user_email' "]
PS: I faced an issue with required hidden fields when I tried to use this dynamichidden*, the shortcode appears on my website as a plain text.
Turns out, i wasn't using the shortcode correctly.
These are the shortcodes i've used in with Contact Form 7 Dynamic text extension:
Naam* <br />
[dynamictext* your-name 'CF7_get_current_user']
Email*
[dynamictext* your-email "CF7_get_current_user key='user_email' "]
Let's say you have an PHP array $gigs, and you want to display it as a drop down list in your form like this:
<label>
<strong>Choose your gig</strong>
[select upcoming-gigs data:gigs]
</label>
[submit]
Simply add this to your functions.php file
add_filter('wpcf7_form_tag_data_option', function($n, $options, $args) {
if (in_array('gigs', $options)){
$gigs = array(
"MAY 07 - NEW ORLEANS, LA",
"MAY 09 - AUSTIN, TX",
"MAY 12 - HOUSTON, TX"
);
return $gigs;
}
return $n;
}, 10, 3);
Now your drop down field should look like this:
Source: https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/
I have done this before. Instead of using the PHP code as a placeholder. Use jQuery, for example:
$firstname = $current_user->user_firstname;
<script>
$("input").val("<?php echo $firstname; ?>")
</script>