Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

enter image description here enter image description here

share|improve this question

closed as off-topic by birgire, ialocin, mrwweb, Pieter Goosen, Johannes Pille Aug 31 at 15:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – birgire, ialocin, mrwweb, Pieter Goosen, Johannes Pille
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
A Question (or Answer) shouldn't depend on reading external sites, especially when they are pages long. –  brasofilo Jan 9 '13 at 9:46
1  
Is there actually anything that needs to be done? I just created a custom field in ACF, created a form in GF, added a "custom field", picked the ACF field, saved the form, and added it to a page. I submitted a form post, ticking three boxes, and it saved into the custom field on a new post. Why do you think you need any code at all? –  webaware Jan 9 '13 at 10:12
1  
Is that with, or without, the code you've quoted in your question? I used no additional code, and when I view the custom fields on the new post, it has a value for each checkbox ticked for that field. Using latest versions of WP, ACF, GF. –  webaware Jan 9 '13 at 10:17
1  
Interesting, I had it set for no metabox, just changed to using a metabox and it didn't save. Will play some more. –  webaware Jan 9 '13 at 10:25
1  
In fact, it's saving the values into a "regular" custom field of the same name; in the post screen options, view "Custom Fields". –  webaware Jan 9 '13 at 10:27

2 Answers 2

up vote 1 down vote accepted

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:

  1. Create a ACF custom field group and add a field with the same checkboxes and use the same meta_key as your gravity form.

  2. 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)

    add_action("gform_after_submission_YOUR_FORM_ID", "acf_post_submission", 10, 2);
    
    function acf_post_submission ($entry, $form)
    {
       $post_id = $entry["post_id"];
       $values = get_post_custom_values("YOUR_CUSTOM_FIELD", $post_id);
       update_field("ACF_FIELD_KEY", $values, $post_id);
    }
    
share|improve this answer
    
I used the first answer to solve the problem on another site but this didn't require using the data in the post, just capturing it. I'm now working on a new project that requires the post data and your solution worked great. –  Rob Feb 25 '13 at 14:10
    
Hi @Andy, will this same function apply with a repeater field? I'm trying to follow your steps but I cannot get it working with a repeater. –  Jaypee Feb 18 at 20:39

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:

$formID = 1;    // replace with ID of your form
add_filter("gform_post_data_$formID", 'wpse_78826_gformPostData', 10, 3);

/**
* dirty hack to write checkbox field values as serialised arrays
* to please Advanced Custom Fields, which is _doing_it_wrong()
* @param array $post_data not really the $_POST data, more like a summary of it
* @param array $form the GF form "object"
* @param array $lead the GF lead / entry "object"
* @return array
*/
function wpse_78826_gformPostData($post_data , $form, $lead) {
    $post_data['post_custom_fields']['checkboxen'] = serialize(explode(',', $post_data['post_custom_fields']['checkboxen']));

    return $post_data;
}

Job is done. Nasty hack, but ACF is happy at least.

share|improve this answer
    
It didn't work initially but played around with it and got it working. I think the important part is on the GF custom field name I needed to put in "checkboxen" rather than select an existing custom field name created by ACF. It's now working, thanks very much for all the help. You've made me and about 4 other people in the ACF forum very happy!!! –  Rob Jan 10 '13 at 9:44
1  
Excellent! Regarding the field name, GF is looking for an existing post with custom fields to pick up the ACF field name, so it might have been that you had an old one there. Otherwise, brand me clueless. At least you can now get it working :) –  webaware Jan 10 '13 at 10:07
    
@webaware Your solution work but I'm running into one other problem. This solution saves the data as array and populates the correct checkboxes in the ACF fields. How ever, ACF needs to convert the strings to correct ACF values and this only happens when accessing the admin and a save or publish gets executed by the user. Is there any way to trigger the WP Save automatically? Edit: Sorry, dont know to answer in the correct thread.. –  Andy Jan 16 '13 at 10:41
    
Can you edit this to explain better what "correct ACF values" means? What isn't working? If it's your template, then maybe you'll just need to modify how you're retrieving the values to cope with the fact that the kludged values are in a slightly different format. –  webaware Jan 16 '13 at 12:33
    
@Andy Please do not use answers for discussion. You will be able to leave comments when you gain some reputation on site. –  Rarst Jan 16 '13 at 13:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.