Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active March 20, 2025 16:33
Show Gist options
  • Save spivurno/32e914d67723f89717be2904ce6234c4 to your computer and use it in GitHub Desktop.
Save spivurno/32e914d67723f89717be2904ce6234c4 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Populate Form with Entry (Optionally Update Entry on Submission)
<?php
/**
* Gravity Wiz // Gravity Forms // Populate Form with Entry (Optionally Update Entry on Submission)
*
* Pass an entry ID and populate the form automatically. No form configuration required. Optionally update the entry on
* submission.
*
* @version 1.5
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
* @see https://www.screencast.com/t/SdNyfbNyC5C
*
* Plugin Name: Gravity Forms - Populate Form w/ Entry
* Plugin URI: http://gravitywiz.com/
* Description: Pass an entry ID and populate the form automatically. No form configuration required. Optionally update the entry on submission.
* Author: Gravity Wiz
* Version: 1.4
* Author URI: http://gravitywiz.com
*/
class GW_Populate_Form {
public function __construct( $args = array() ) {
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'query_arg' => 'eid',
'update' => false,
) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_filter( 'gform_form_args', array( $this, 'prepare_field_values_for_population' ) );
add_filter( 'gform_entry_id_pre_save_lead', array( $this, 'set_submission_entry_id' ), 10, 2 );
}
public function prepare_field_values_for_population( $args ) {
if( ! $this->is_applicable_form( $args['form_id'] ) || ! $this->get_entry_id() ) {
return $args;
}
$entry = GFAPI::get_entry( $this->get_entry_id() );
if( is_wp_error( $entry ) ) {
return $args;
}
$args['field_values'] = $this->prepare_entry_for_population( $entry );
add_filter( sprintf( 'gform_pre_render_%d', $args['form_id'] ), array( $this, 'prepare_form_for_population' ) );
return $args;
}
public function prepare_form_for_population( $form ) {
foreach( $form['fields'] as &$field ) {
$field['allowsPrepopulate'] = true;
if( is_array( $field['inputs'] ) ) {
$inputs = $field['inputs'];
foreach( $inputs as &$input ) {
$input['name'] = (string) $input['id'];
}
$field['inputs'] = $inputs;
}
$field['inputName'] = $field['id'];
}
return $form;
}
public function prepare_entry_for_population( $entry ) {
$form = GFFormsModel::get_form_meta( $entry['form_id'] );
foreach( $form['fields'] as $field ) {
if( $field->type == 'post_category' ) {
$value = explode( ':', $entry[ $field->id ] );
$entry[ $field->id ] = $value[1];
}
switch( GFFormsModel::get_input_type( $field ) ) {
case 'checkbox':
$values = $this->get_field_values_from_entry( $field, $entry );
if( is_array( $values ) ) {
$value = implode( ',', array_filter( $values ) );
} else {
$value = $values;
}
$entry[$field['id']] = $value;
break;
case 'list':
$value = maybe_unserialize( rgar( $entry, $field->id ) );
$list_values = array();
if( is_array( $value ) ) {
foreach( $value as $vals ) {
if( is_array( $vals ) ) {
$vals = implode( '|', array_map( function( $value ) {
$value = str_replace( ',', '&#44;', $value );
return $value;
}, $vals ) );
}
array_push( $list_values, $vals );
}
$entry[ $field->id ] = implode( ',', $list_values );
}
break;
}
}
return $entry;
}
public function get_field_values_from_entry( $field, $entry ) {
$values = array();
foreach( $entry as $input_id => $value ) {
$fid = intval( $input_id );
if( $fid == $field['id'] )
$values[] = $value;
}
return count( $values ) <= 1 ? $values[0] : $values;
}
public function set_submission_entry_id( $entry_id, $form ) {
if( ! $this->_args['update'] || ! $this->is_applicable_form( $form['id'] ) || ! $this->get_entry_id() ) {
return $entry_id;
}
add_filter( 'gform_use_post_value_for_conditional_logic_save_entry', '__return_true' );
return $this->get_entry_id();
}
public function get_entry_id() {
return rgget( $this->_args['query_arg'] );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return ! $this->_args['form_id'] || $form_id == $this->_args['form_id'];
}
}
# Configuration
new GW_Populate_Form( array(
'update' => true
) );
@codegold79
Copy link

codegold79 commented May 17, 2018

I tried this script, but it doesn't seem to save changes I make to radio buttons. I wonder if it's a problem because of the version of Gravity Forms I'm using which is 2.3.1.

UPDATE 6/1/2018:
Thank you to David Smith for version 1.3. It fixed the problem I was having with the radio buttons not updating.

@rms2048
Copy link

rms2048 commented Jun 19, 2018

works fine for me, but I don´t manage to get it to work together with the plugin gravity wiz gp limit submissions. Only counts one.

@aegenes
Copy link

aegenes commented Jul 2, 2018

I noticed that this doesn't seem to work with file upload fields...

@jakelunniss
Copy link

Could this be modified to automatically update an existing entry, by putting the entry ID into a hidden field?

@n4naveed
Copy link

n4naveed commented May 8, 2019

Your code works perfect but it does not show file uploads when editing gravity form. Can you help? Let me know.

@ginodp
Copy link

ginodp commented Mar 20, 2025

Thanks for this wonderfull script. I only have one question. It loads the data up really nice but it doesnt reload the checkbox answers. Is this something I can fix myself?

@spivurno
Copy link
Author

@ginodp Possibly something AI could fix for you! This is pretty old code though. If you wanted a supported solution, Easy Passthrough is the ticket.

https://gravitywiz.com/documentation/gravity-forms-easy-passthrough/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment