Support

Account

Home Forums General Issues Check box values from gravity form not stored in ACF check box

Solving

Check box values from gravity form not stored in ACF check box

  • I have a checkbox field with field label “Contributions” in gravity form and an ACF custom field with label “Contributions”. A gravity form is created for user registration. The data entered is used to populate the profile by using the gravity form User Registration Feeds.

    There is no issue for the text fields. But if it is a check box field in gravity form, the values selected are not captured in the user’s profile. Single value selected is in the user’s profile but not for multiple values.

    How do I get the multiple selected values from gravity form to ACF?

    Thanks.

  • Here how to do it :
    (https://docs.gravityforms.com/gform_advancedpostcreation_post_after_creation/)

    The scope of this example is limited to form id 1 and field id 18, you need to update these values to apply to your own form and field.
    Replace my_custom_field_key with your custom field meta key

    add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
    function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
     
        // Checkboxes field id.
        $field_id = 18;
     
        // Get field object.
        $field = GFAPI::get_field( $form, $field_id );
      
        if ( $field->type == 'checkbox' ) {
            // Get a comma separated list of checkboxes checked
            $checked = $field->get_value_export( $entry );
      
            // Convert to array.
            $values = explode( ', ', $checked );
      
        }
     
        // Replace my_custom_field_key with your custom field meta key.
        update_post_meta( $post_id, 'my_custom_field_key', $values );
    }
  • When using @franckcode‘s code above it would be better to use the acf function update_field() and to use the field key

    
    update_field($field_key, $values, $post_id);
    
  • I’d like to chime in on this, as I just had this same issue and used a slightly different code to resolve it. We are using GF to create a custom post type (with the help of “Gravity Forms + Custom Post Types” plugin by Gravity Wiz) with several custom fields that are multiple checkboxes. The code posted above did NOT work for me, nor did the code provided directly by Gravity Forms here https://docs.gravityforms.com/gform_after_create_post/#3-update-a-post-custom-field-with-serialized-gf-checkboxes

    We had to change a couple of important things to get this to work. First, rather than add_filter we changed that to add_action since in their documentation, gform_after_create_post is an action https://docs.gravityforms.com/gform_after_create_post/

    Second, we changed the post type that you test for to ‘post_custom_field’ — I suspect this might be because we’re using Gravity Forms + Custom Post Types which helps to map the checkboxes to the ACF custom field.

    So here is what we ended up with that DID work (maybe someone else will be helped by this)

    add_action( 'gform_after_create_post_3', 'gf_post_acf_checkboxes', 10, 3 );
    function gf_post_acf_checkboxes( $post_id, $entry, $form ) {
     
        // Checkboxes field id. Change this value to your field id number.
        $field_id = 18;
        
     
        // Get field object.
        $field = GFAPI::get_field( $form, $field_id );
       
     
        if ( $field->type == 'post_custom_field' ) {
            // Get a comma separated list of checkboxes checked
            $checked = $field->get_value_export( $entry );
     
            // Convert to array.
            $values = explode( ', ', $checked );
            
    
        }
     
        // Replace my_custom_field_key with your custom field meta key.
        update_post_meta( $post_id, 'products', $values );
    }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.