Created
August 14, 2017 20:41
-
-
Save zackkatz/7f8d926e5d9fad23b53783e4a26be55f to your computer and use it in GitHub Desktop.
Gravity Forms - Modify field value after updating in Gravity Forms OR GravityView
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 | |
/** | |
* Modify a field value after updating an entry | |
* | |
* @param array $form Gravity Forms form object | |
* @param int $entry_id ID of the entry that was updated | |
* @param array $original_entry Original entry object, before update | |
* | |
* @return void | |
*/ | |
function gv_custom_gform_after_update_entry( $form = array(), $entry_id = 0, $original_entry = array() ) { | |
// Get the current entry | |
$updated_entry = GFAPI::get_entry( $entry_id ); | |
// Set a new value for Field #5 | |
$field_id_or_meta_key_to_edit = '5'; | |
// Define the new value here | |
$new_value = 'New Value'; | |
$updated_entry[ $field_id_or_meta_key_to_edit ] = $new_value; | |
// Save the update | |
$updated = GFAPI::update_entry( $updated_entry ); | |
if ( ! $updated ) { | |
gf_logging()->log_error( __FUNCTION__ . ': Entry failed to update' ); | |
} elseif ( is_wp_error( $updated ) ) { | |
gf_logging()->log_error( __FUNCTION__ . ': Entry failed to update: ' . $updated->get_error_message() ); | |
} | |
} | |
add_action( 'gform_after_update_entry', 'gv_custom_gform_after_update_entry', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for posting this. Very useful for keeping gravity forms and a database in sync.