I’m trying to feed checkbox values from a Gravity Forms form (which creates a new post) into an Advanced Custom Fields field. I’ve had a read around and found some info in a post (at the bottom of the question).
Is this the correct way to do it? It’s not inserting the multiple GF checkboxes into the ACF textbox. Should I be creating checkboxes with the same options in ACF? Here’s my code:
add_action("gform_post_submission", "acf_post_submission", 10, 2);
function acf_post_submission ($entry, $form)
{
$post_id = $entry["post_id"];
$values = get_post_custom_values("submit_intended", $post_id);
update_field("field_23", $values, $post_id);
}
I’ve also tried the following which relates to the screenshots:
add_action("gform_post_submission", "checkbox_arrays", 10, 2);
function checkbox_arrays($entry, $form)
{
$post_id = $entry["post_id"];
$form_id = $entry["form_id"];
if ($form_id==3)
{
$values = get_post_custom_values("submit_gf_intended", $post_id);
add_post_meta($post_id, "submit_intended", $values);
}
}
This post in the advanced custom fields forum may help – http://support.advancedcustomfields.com/discussion/544/acf-and-gravity-forms/p1
I’ve attached some screenshots too.
My situation to tackle this problem was a bit problematic because I wanted to use the GF Update Post plugin to let my users edit their post right after they submitted the content. With the above solution ACF does not write to the db and correct ACF fields (at least not for me).
My solution:
Create a ACF custom field group and add a field with the same checkboxes and use the same meta_key as your gravity form.
Use a function after submission to fetch the all matching keys as array and update the ACF field key (Show on screen -> ACF field value in ACF field group edit screen)
I had a look at this again this morning, and I reckon your problem and solution are both very simple.
NB: using current versions — WordPress 3.5, Advanced Custom Fields 3.5.7.2, Gravity Forms 1.6.11
First, “ordinary” fields like text fields require no additional effort; create in ACF, create a post that uses it so that it’s there to be found by GF, add to a GF form, and use. The value is written to the field. Job is done. No additional code required.
Now, for checkboxes, multiple ticks means multiple values to add to the post. GF does what you expect a sane WordPress plugin to do, it creates multiple entries in the postmeta table. But that’s not what ACF wants. When you edit a post and tick multiple checkboxes on an ACF field, what is saved is a single postmeta record with a serialised array. Nice.
So, to “fix” what GF is doing so that it’s broken in the way that ACF wants it, get the ID of your form and add a filter to kludge up the post data before GF saves it to postmeta:
Job is done. Nasty hack, but ACF is happy at least.