Last active
January 22, 2024 10:54
-
-
Save uamv/a7958dc9df37ca41514a4bddb697497a to your computer and use it in GitHub Desktop.
Save Gravity Form field data to post meta by adding a class to the field.
This file contains hidden or 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 | |
/** | |
* Save Gravity Form field data to post meta by adding a class to any field. | |
* Meta is mapped so that it is readable by Advanced Custom Fields | |
* Format class as… | |
* post_meta-{meta_key} -- for simple fields & writing lists as array to single meta record | |
* post_meta-{meta_key}-.3 -- for multi-input fields | |
* post_meta-{repeater_key}-1.{meta_key} -- for list fields mapped to meta readable by ACF | |
*/ | |
// Run this function after the Gravity Form has created a post | |
add_action( 'gform_after_create_post', function( $post_id, $entry, $form ) { | |
// Loop through the forms fields | |
foreach( $form['fields'] as &$field ) { | |
// If class does not call a post_meta save, then ignore it. | |
if ( strpos( $field['cssClass'], 'post_meta-' ) === false ) | |
continue; | |
// Find all post_meta save calls within the class | |
preg_match_all( '/post_meta-[\S]+/', $field['cssClass'], $post_meta_save_calls ); | |
// Loop through the post_meta save calls | |
foreach ( $post_meta_save_calls[0] as $i => $post_meta_save_call ) { | |
// Remove the post_meta- identifier | |
$post_meta = str_replace( 'post_meta-', '', $post_meta_save_call ); | |
// Find any complex GF to post_meta mapping and do things if any exist | |
if ( preg_match( '/-\.\d+/', $post_meta, $complex_post_meta_mapping ) ) { | |
// Get the Gravity Form input identifier and the post meta key | |
$post_meta_map = explode( '-', $post_meta ); | |
// Add the post meta for the complex mapping | |
add_post_meta( $post_id, $post_meta_map[0], $entry[ $field['id'] . $post_meta_map[1] ], true ); | |
} else { | |
// Check the type of Gravity Form field and process accordingly | |
switch ( $field['type'] ) { | |
case 'list': | |
// Get the Gravity form value for this list and maybe_unserialize it | |
$value = maybe_unserialize( $entry[ $field['id'] ] ); | |
// If columns have been identified, then process meta to be readable by ACF | |
if ( preg_match( '/-\d+\./', $post_meta, $complex_post_meta_mapping ) ) { | |
// Count how many rows are in the list | |
$count = count( $value ); | |
// Get the Gravity Form column identifiers and the post meta key | |
$post_meta_map = explode( '-', $post_meta ); | |
// Pull the top-level meta key from the array, leaving column meta keys | |
$post_meta_key = array_shift( $post_meta_map ); | |
// initalize column key array | |
$column_keys = array(); | |
// Populate column key array | |
foreach ( $post_meta_map as $column ) { | |
$column = explode( '.', $column ); | |
$key = (int) $column[0] - 1; | |
$column_keys[] = $column[1]; | |
} | |
// If multiple column list | |
if ( ! is_null( $value ) && is_array( $value[0] ) ) { | |
foreach ( $value as $row_key => $row ) { | |
$row = array_values( $row ); | |
foreach ( $row as $column_key => $data ) { | |
add_post_meta( $post_id, $post_meta_key . '_' . $row_key . '_' . $column_keys[ $column_key ], $data, true ); | |
} | |
} | |
// If single column list | |
} else if ( ! is_null( $value ) ) { | |
foreach ( $value as $row_key => $data ) { | |
add_post_meta( $post_id, $post_meta_key . '_' . $row_key . '_' . $column_keys[0], $data, true ); | |
} | |
} | |
// Add count of rows, so ACF can properly read the meta data | |
add_post_meta( $post_id, $post_meta_key, $count, true ); | |
} else { | |
// If we've not defined column keys, then simply write the list data as an array to a single meta record | |
add_post_meta( $post_id, $post_meta, $value, true ); | |
} | |
break; | |
case 'checkbox': | |
$value = array(); | |
// Loop through the Gravity Forms entry | |
foreach ( $entry as $key => $data ) { | |
// If entry record is an option belonging to specific checkbox field then add it to our value array | |
if ( strpos( (string) $key, (string) $field['id'] ) === 0 ) { | |
$value[ $key ] = $data; | |
} | |
} | |
// Re-index the array | |
$value = array_values( $value ); | |
// Add the post meta | |
add_post_meta( $post_id, $post_meta, $value ); | |
break; | |
default: | |
// Add the post meta for non-complex mapping | |
add_post_meta( $post_id, $post_meta, $entry[ $field['id'] ], true ); | |
break; | |
} | |
} | |
} | |
} | |
return $form; | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment