Last active
February 29, 2020 16:36
-
-
Save vfontjr/fdc8320d4767a90e5371dd119e2fe6a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('frm_after_create_entry', 'populate_acf_repeater', 30, 2); | |
function populate_acf_repeater( $entry_id, $form_id ) { | |
/* make sure we're working with the correct form * | |
* change 30 to the ID of your form */ | |
if ( $form_id == 30 ) { | |
/* get the entry header from wp_frm_items */ | |
$entry = FrmEntry::getOne( $entry_id ); | |
/* if this entry has no post, no reason to keep going */ | |
if ( ! $entry->post_id ) { | |
return; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('frm_after_create_entry', 'populate_acf_repeater', 30, 2); | |
function populate_acf_repeater( $entry_id, $form_id ) { | |
/* make sure we're working with the correct form | |
* change 30 to the ID of your form | |
*/ | |
if ( $form_id == 30 ) { | |
/* get the entry header from wp_frm_items */ | |
$entry = FrmEntry::getOne( $entry_id ); | |
/* if this entry has no post, no reason to keep going */ | |
if ( ! $entry->post_id ) { | |
return; | |
} | |
/* retrieve the repeater and row field ids by their keys | |
* this prevents issues when migrating code to | |
* new WordPress instance | |
*/ | |
$repeater_id = FrmField::get_id_by_key( "demo_repeater" ); | |
$name_field_id = FrmField::get_id_by_key( "demo_name" ); | |
$city_field_id = FrmField::get_id_by_key( "demo_city" ); | |
$state_field_id = FrmField::get_id_by_key( "demo_state" ); | |
/* store the ACF repeater and fields names to variables for use in the loop */ | |
$acf_repeater = 'repeater'; | |
$acf_repeater_name = 'name'; | |
$acf_repeater_city = 'city'; | |
$acf_repeater_state = 'state'; | |
/* this returns the unserialized array of the repeater content | |
* that is stored as a serialized array in the wp_frm_item_metas table | |
*/ | |
$repeater_meta_value = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $repeater_id ); | |
/* get the number of rows for the ACF repeater field meta value */ | |
$number_rows = count( $repeater_meta_value ); | |
/* we can write the ACF repeater header record now */ | |
add_post_meta( $entry->post_id, $acf_repeater, $number_rows ); | |
/* let's loop through the array to get the data, format it, and write it to wp_postmeta | |
* the $loop_counter is necessary because it is part of the meta_key for each ACF row item | |
*/ | |
$loop_counter = 0; | |
foreach( $repeater_meta_value as $repeater_row_item ) { | |
/* format the meta_keys for the ACF repeater */ | |
$name_meta_key = $acf_repeater . '_' . $loop_counter . '_' . $acf_repeater_name; | |
$city_meta_key = $acf_repeater . '_' . $loop_counter . '_' . $acf_repeater_city; | |
$state_meta_key = $acf_repeater . '_' . $loop_counter . '_' . $acf_repeater_state; | |
/* retrieve the Formidable repeater field values | |
* $repeater_row_item is the entry id for each row entry | |
*/ | |
$name_meta_value = FrmEntryMeta::get_entry_meta_by_field( $repeater_row_item, $name_field_id ); | |
$city_meta_value = FrmEntryMeta::get_entry_meta_by_field( $repeater_row_item, $city_field_id ); | |
$state_meta_value = FrmEntryMeta::get_entry_meta_by_field( $repeater_row_item, $state_field_id ); | |
/* write the ACF values to the wp_postmeta table */ | |
add_post_meta( $entry->post_id, $name_meta_key, $name_meta_value ); | |
add_post_meta( $entry->post_id, $city_meta_key, $city_meta_value ); | |
add_post_meta( $entry->post_id, $state_meta_key, $state_meta_value ); | |
/* increment the $loop_counter for the next row */ | |
$loop_counter++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment