Multiple checkbox fields on Gravity form to ACF check boxes

Hello there,

Newbie here so please be gentle. I’m trying to link a gravity form to ACF fields with checkboxes. I can get the code below to work with a single field, but how do I get multiple checkbox fields on the same form to update? If I try to duplicate the function only one works. And I tried to submit an array but I suspect my PHP knowledge was simply lacking. Any help much appreciated.

This is the code I can get working on a single check box field but no more.

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 = 8;
    $form = 1;
 
    // 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, 'interests', $values );
}

Thanks,

Mat

How did you duplicate the function? Can you share the version that was not working for you?

Hi Chris,

Thanks for responding. I know I was probably being idiotic but I just repeated the code snippet with different names and variables. See below…

Snippet 1:

add_filter( 'gform_advancedpostcreation_post_after_creation_4', 'apc_serialize_checkboxes_2', 10, 4 );
function apc_serialize_checkboxes_2( $post_id, $feed, $entry, $form ) {
 
    // Checkboxes field id.
    $field_id = 37;
    $form = 4;
 
    // 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, 'education_level', $values );
}

Snippet 2

add_filter( 'gform_advancedpostcreation_post_after_creation_5', 'apc_serialize_checkboxes_3', 10, 4 );
function apc_serialize_checkboxes_3( $post_id, $feed, $entry, $form ) {
 
    // Checkboxes field id.
    $field_id_2 = 24;
    $form = 4;
 
    // Get field object.
    $field_2 = GFAPI::get_field( $form, $field_id_2 );
  
    if ( $field_2->type == 'checkbox' ) {
        // Get a comma separated list of checkboxes checked
        $checked_2 = $field_2->get_value_export( $entry );
  
        // Convert to array.
        $values_2 = explode( ', ', $checked2 );
  
    }
 
    // Replace my_custom_field_key with your custom field meta key.
    update_post_meta( $post_id, 'interests', $values_2 );
}

The second snippet doesn’t work.

And I also made an attempt to combine the functions and update the post meta with an array but this is well outside my PHP comfort zone - but I suspect this is the proper and scalable way to do this…

Snippet 3

add_filter( 'gform_advancedpostcreation_post_after_creation_5', 'apc_serialize_checkboxes_2', 10, 4 );
function apc_serialize_checkboxes_2( $post_id, $feed, $entry, $form ) {
 
    // Checkboxes field id.
    $field_id = 24;
    $field_2_id = 27;
    $form = 4;
 
    // Get field object.
    $field = GFAPI::get_field( $form, $field_id );
    $field_2 = GFAPI::get_field( $form, $field_2_id );
  
    if ( $field->type == 'checkbox' ) {
        // Get a comma separated list of checkboxes checked
        $checked = $field->get_value_export( $entry );
         $checked_2 = $field_2->get_value_export( $entry );
  
        // Convert to array.
        $values = explode( ', ', $checked );
         $values_2 = explode( ', ', $checked_2 );
  
    }
    
    $metaValues = array(
    'interests' => '$values',
    'education_level' => '$values_2', 
);

// Set all key/value pairs in $metaValues
foreach ($metaValues as $metaKey => $metaValue) {
    update_post_meta($postId, $metaKey, $metaValue);
}

Any pointers suggestions would be much appreciated.

Thanks,

Mat

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.