Wordpress Gravity Forms get List values

≡放荡痞女 提交于 2019-12-07 12:00:55

问题


Can somebody point me in the direction on how to extract values from submitted forms containing list fields?

I'm trying to create a front-end posting form using Gravity Forms, and then having the submitted values be assigned to custom fields made with Advanced Custom Fields.

For normal fields, you can do this with the following:

add_action("gform_after_submission_1", "acf_submission", 10, 2);

function acf_submission($entry, $form)
{
   $post_id = $entry["post_id"];
   update_field('field_###', $entry['#'], $post_id ); 
   update_field('field_###', $entry['#'], $post_id ); 
   update_field('field_###', $entry['#'], $post_id );  
}

Where field_### is the ACF custom field key, entry['#'] is the Gravity Forms field ID, and $post_id is the id of the post you want to update/store values to.

Using entry['#'] works great with normal fields (text, paragraph, etc.), but list (repeater) fields are stored a bit differently. In the DB, the value looks like this (for a list field that has 3 fields (columns), and for somebody who clicked the add button to add an additional instance of the list field:

a:2:{i:0;a:3:{s:4:"Column 1 Name";s:7:"value input";s:6:"Column 2 Name";s:34:"value input";s:11:"Column 3 Name";s:24:"value input";}i:1;a:3:{s:4:"Column 1 Name";s:11:"value input";s:6:"Column 2 Name";s:19:"value input";s:11:"Column 3 Name";s:22:"value input";}}

I can't figure out how to extract those values and assign them to ACF fields. I tried entry['#.#'] as you would do for fields like address, but that didn't work.


回答1:


This is a serialized array so you can use the php function unserialize to extract the values.

$array_values = unserialize($entry['#']);  
print_r($array_values); //see what your values are.

Now that you have your values you can access them in your new $array_values array. Getting one value out of a serialized array in PHP




回答2:


Information can be found from Gravity Form's Documentation - GF_Field_List

$list_values = unserialize( rgar( $entry, '3' ) ); 

// You will get an array like below
$list_values = array(
  array(
    'Column 1' => 'one',
    'Column 2' => 'two',
    'Column 3' => 'three',
  ),
  array(
    'Column 1' => 'i',
    'Column 2' => 'ii',
    'Column 3' => 'iii',
  ),
  array(
    'Column 1' => '1',
    'Column 2' => '2',
    'Column 3' => '3',
  ),
);


来源:https://stackoverflow.com/questions/24269983/wordpress-gravity-forms-get-list-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!