Gravity Forms plugin is a very popular WordPress plugin. I am a big fan of this fabulous plugin. One this disappoints me while working on one of my projects based on Gravity Form.
Gravity Form not offering the entry edit feature by default, especially for the frontend. You’ve probably wanted an easy way to edit the entry. Gravity Form cannot often retrieve a previous entry, let alone make edits to that entry.
I am going to show you an easy way to add that capability to any of your Gravity Forms with a custom filter.
1. The first step is to make a duplicate form. Which form you want to edit from the frontend. It helps you edit the entry, not conflict with the original form, you can remove those fields as which you don’t want to edit.
2. Place the duplicated form shortcode on the edit page, similar you did for the original form.
3. Make the form field values pre-populated. How to do this, just check out the following code.
add_filter( 'gform_form_args', 'prefix_gf_form_populate', 10, 1 ); function prefix_gf_form_populate( $form_args ){ // Replace you form id with value 2. if ($form_args['form_id'] == 2) { // $_REQUEST['entry_id'] replace this value with you entry_id, if it is query string. Change query string parameter. $entry_id = $_REQUEST['entry_id']; $entry = GFAPI::get_entry( $entry_id ); if( is_wp_error( $entry ) ) return; $form_args['field_values'] = $entry; $form_args['form_id'] = 2; // Replace you form id with value 2. return $form_args; } }
4. Create a hidden field to store the entry id in duplicate form. This hidden field will carry the edit entry id. This is code for pre-populate the hidden field value.
add_filter( 'gform_field_value_hidden_entry_id', 'prefix_gf_population_hidden_entry_id' ); function prefix_gf_population_hidden_entry_id( $value ) { if( isset($_REQUEST['abstract_id']) && $_REQUEST['abstract_id'] != ''){ return $_REQUEST['abstract_id']; } else { return $value; } }
5. Form pre-submission filter, This filter is the main function, it will update the entry data.
add_action("gform_pre_submission_2", "prefix_gf_update_form_entry"); function prefix_gf_update_form_entry( $form ) { $entry_id = rgpost("input_5"); // hidden field value of the form. $editentry = GFAPI::get_entry($entry_id); if( is_wp_error( $editentry ) ) return; $editentry = GFAPI::get_entry($entry_id); $editentry['1'] = rgpost("input_1"); $editentry['2'] = rgpost("input_2"); $editentry['3'] = rgpost("input_3"); $editentry['4'] = rgpost("input_4"); $result = GFAPI::update_entry( $editentry ); }
6. Remove the duplicate form entries to store in the database. Because we are editing the entries, so we don’t need to store the duplicate form entries.
add_action( 'gform_after_submission_2', 'prefix_gf_remove_form_entry' ); function prefix_gf_remove_form_entry( $entry ) { GFAPI::delete_entry( $entry['id'] ); }
Place all functions and code snippet in current active theme functions.php file.