Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress: Gravity Forms List to ACF Repeater post save
<?php
add_action('gform_after_submission', 'gfToAcfListToRepeater', 10, 2);
function gfToAcfListToRepeater($entry, $form)
{
foreach ($form['fields'] as $field) {
if (!($field['type'] == 'post_custom_field' && $field['inputType'] == 'list' && $field['enableColumns'] == true)) {
continue;
}
$id = $field['id'];
$postId = $entry['post_id'];
$acfFields = unserialize($entry[$id]);
$fieldName = $field['postCustomFieldName'];
$count = 0;
foreach ($acfFields as $item) {
foreach ($item as $key => $value) {
$acfKey = str_replace(' ', '_', strtolower($key));
$acfFieldName = $fieldName . '_' . $count . '_' . $acfKey;
update_post_meta($postId, $acfFieldName, $value);
}
$count++;
};
update_post_meta($postId, $fieldName, count($acfFields));
}
}
@tmoitie
Copy link
Author

tmoitie commented Mar 27, 2014

Caution, contains nested loops. May cause explosions.

@lahirusn
Copy link

lahirusn commented Oct 18, 2014

Man You Saved my day!! Thank you So much for this snippet. I had to customize it to support with my specific requirement but you gave me a very good starting point when i was at a complete loss. Thanks again. Great Job! :D 👍

@EMembrillas
Copy link

EMembrillas commented Mar 16, 2015

Hi! Perfect!!

I have made some changes in the code, because the database will double the $fieldName for each value that is created. This way you only creates a $fieldName with the number of fields that must appear. a Greeting

add_action('gform_after_submission', 'gfToAcfListToRepeater', 10, 2);
function gfToAcfListToRepeater($entry, $form)
{ foreach ($form['fields'] as $field) {
if (!($field['type'] == 'post_custom_field' && $field['inputType'] == 'list' && $field['enableColumns'] == true)) {
continue;
}
$id = $field['id'];
$postId = $entry['post_id'];
$acfFields = unserialize($entry[$id]);
$fieldName = $field['postCustomFieldName'];
$count = 0;

    foreach ($acfFields as $item) {
        foreach ($item as $key => $value) {
            $acfKey = str_replace(' ', '_', strtolower($key));
            $acfFieldName = $fieldName . '_' . $count . '_' . $acfKey;
            update_post_meta($postId, $acfFieldName, $value);
        }
        $count++;
        delete_post_meta($postId, $fieldName, $value);
    };

   add_post_meta($postId, $fieldName, count($acfFields));

}

}

@anybodesign
Copy link

anybodesign commented Jan 5, 2017

Hello,
I'm very interested by your snippet but I fail using it :(

I have a repeater field (id: _10_memories) with only one subfield (_memory)
and with gravityforms I only use the list field without multiple columns
the values aren't mapped to the ACF repeater… I'm not sure what I have to customize in your snippet ?

I tried this line:
if (!($field['type'] == 'post_custom_field' && $field['inputType'] == 'list'))

and this line:
$fieldName = $field['_memory'];

but it seems it's not enough…
I'd be so pleased if you could help me!

@nicolepaton
Copy link

nicolepaton commented May 4, 2017

Hello.

I am also interested in this snippet but like @anybodydesign I must be missing something about how to use it.

Can you please provide comments in the code about where exactly we should replace the text or a variable with our gravity forms form ID, Gravity Forms list field ID, ACF repeater field ID or name or whatever else is required to be replaced for this snipped for it to work in each case.

I am using the gravity forms user registration plugin and I can easily map all other form fields over to the ACF fields I have created for the user however I cannot get the Gravity forms list to map over correctly to the ACF repeater field in the user meta which is to contain the entered data.

Thank you very much,
Nicole

@andy-d-guru
Copy link

andy-d-guru commented Jun 24, 2018

Did anyone actually get it to work? Can someone help with it? My custom repeater field name is "app_lvl_data".
Shall I replace it here: $fieldName = $field['postCustomFieldName']; ?

@ktrusak
Copy link

ktrusak commented Dec 4, 2019

Here is a much simpler solution that uses the ACF functions:

`add_action( 'gform_after_submission_36', 'list_field_to_acf_accolades', 10, 2 );
function list_field_to_acf_accolades( $entry, $form ){

$id = $entry['post_id'];
$acfFields = maybe_unserialize( rgar( $entry, '77' ) );
$val = array();

foreach ( (array)$acfFields as $key => $data) {
	$description = array();
	$description['description'] = $data; 
	$val[] = $description;	
}		
update_field( 'accolades', $val, $id ); 

}`

@incognitozen
Copy link

incognitozen commented Apr 19, 2020

I had the same issues as others and here is updated simple code with instructions

// Change the number in the following line with the id of your form. --- >  gform_after_submission_3. For me the form had id 3
add_action( 'gform_after_submission_3', 'list_field_to_acf_accolades', 10, 2 );
function list_field_to_acf_accolades( $entry, $form ){
$id = $entry['post_id'];
// Change the number 2 in ( rgar( $entry, '2' ) );  with the id of the list field in your gravity form. For me this was number 2. Also pros is the name of my repeater field. My subfield is also called pros.
$acfFields = maybe_unserialize( rgar( $entry, '2' ) );
$val = array();
foreach ( (array)$acfFields as $key => $data) {
$description = array();
$description['pros'] = $data;
$val[] = $description;
}
update_field( 'pros', $val, $id ); 
}

@klkwebservices
Copy link

klkwebservices commented Apr 24, 2020

Incognitozen, could you clarify what your 'pros' is? Is it a CPT slug or is it the field name in Gravity Forms (name assigned to your list field =2 )?

Does anyone know of anything special for a multiple-column repeater field?

Thanks!

@incognitozen
Copy link

incognitozen commented Apr 26, 2020

Incognitozen, could you clarify what your 'pros' is? Is it a CPT slug or is it the field name in Gravity Forms (name assigned to your list field =2 )?

Does anyone know of anything special for a multiple-column repeater field?

Thanks!

pros is the name of my repeater field. My subfield is also called pros.

@dustpickle
Copy link

dustpickle commented Mar 2, 2021

@icognitozen How would you modify this to use a 2 column repeater?

My ACF Repeater is:

Repeater: links
Subfield Col1 = link_title
Subfield Col2 = link_url

@mkormendy
Copy link

mkormendy commented Nov 22, 2021

I prefer the @EMembrillas version, because:

  1. there's less ACF cruft made in your wp_postmeta table that ACF doesn't absolutely need (it rebuilds it all on re-saving the created post in the backend anyhow if you choose to do so);
  2. it uses the default WordPress methods for adding a post custom fields in a well formed multi-column key naming structure that doesn't necessarily have to be used for ACF, but is compatible regardless;
  3. it handles the duplicate issue mentioned.

In my experience, I noticed that the $post_id wasn't available when using the 'gform_after_submission' action. Instead, you can use the 'gform_after_create_post' (or 'gform_after_create_post_FORMID') action hook to make sure you can get the $post_id (if Post Fields exist in your form).

I am also using the Advanced Post Creation GF add-on; it too has an action called 'gform_advancedpostcreation_post_after_creation' (or 'gform_advancedpostcreation_post_after_creation_FORMID') which makes sure the $post_id is available.

I didn't have to go and grab $post_id from the $entry array passed in to the $entry parameter – I was able to use it directly in the code.

add_action('gform_advancedpostcreation_post_after_creation_68', 'partners_list_field_to_acf_repeater', 10, 4);
function partners_list_field_to_acf_repeater($post_id, $feed, $entry, $form) {

  // set these based on form fields and acf/postmeta fields
  $gf_list_field_id = 12;
  $acf_repeater_field_name = 'contacts_list';

  $acf_fields = maybe_unserialize(rgar($entry, $gf_list_field_id));
  $count = 0;
  foreach ($acf_fields as $item) {
    foreach ($item as $key => $value) {
      $acf_key = str_replace(' ', '_', strtolower($key));
      $postmeta_field_name = $acf_repeater_field_name . '_' . $count . '_' . $acf_key;
      update_post_meta($post_id, $postmeta_field_name, $value);
    }
    $count++;
    delete_post_meta($post_id, $acf_repeater_field_name, $value);
  };
  add_post_meta($post_id, $acf_repeater_field_name, count($acf_fields));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment