Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active October 20, 2021 10:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spivurno/103adcb3f31deb666ff4 to your computer and use it in GitHub Desktop.
Save spivurno/103adcb3f31deb666ff4 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Force Default Value
<?php
/**
* WARNING! THIS SNIPPET MAY BE OUTDATED.
* The latest version of this snippet can be found in the Gravity Wiz Snippet Library:
* https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-force-default-value.php
*/
/**
* Gravity Wiz // Gravity Forms // Force Default Value
*
* Force the default value to be captured for fields hidden by conditional logic.
*
* @version 1.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
* @copyright 2015 Gravity Wiz
*/
class GW_Force_Default_Value {
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_ids' => array()
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.9', '>=' ) ) {
return;
}
// carry on
add_filter( 'gform_entry_post_save', array( $this, 'add_default_values_to_entry' ), 10, 2 );
}
public function add_default_values_to_entry( $entry, $form ) {
if( ! $this->is_applicable_form( $form ) ) {
return $entry;
}
$requires_update = false;
/** @var GF_Field $field */
foreach( $form['fields'] as $field ) {
if( ! $this->is_applicable_field( $field ) ) {
continue;
}
if( ! rgar( $entry, $field->id ) && GFFormsModel::is_field_hidden( $form, $field, array(), $entry ) ) {
$value = $field->get_value_default_if_empty( $field->get_value_submission( array(), false ) );
if( ! rgblank( $value ) ) {
$requires_update = true;
$entry[ $field->id ] = $value;
}
}
}
if( $requires_update ) {
GFAPI::update_entry( $entry );
}
return $entry;
}
function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
function is_applicable_field( $field ) {
return empty( $this->_args['field_ids'] ) || in_array( $field->id, $this->_args['field_ids'] );
}
}
# Configuration
new GW_Force_Default_Value( array(
'form_id' => 123,
'field_ids' => array( 4, 5 )
) );
@Koli14
Copy link

Koli14 commented Jun 5, 2017

Thanks for this snippet! I try to use it, and it's works for user-email, and username, but it does not work for first-name and last-name for me.
The values are there in the forms HTML. I use a 'Name' Field. I tried a few things, but can't solve it.
Do you have any idea?

Thanks: Kolos

@joeldbirch
Copy link

In case it helps someone else, @Koli14's issue is likely because the inputs have a disabled attribute on them when hidden which means they are not submitted.

@spivurno
Copy link
Author

spivurno commented Apr 7, 2020

@joeldbirch This snippet accounts for that scenario and actually fetches the default value directly from the field property (not the $_POST). It looks like it just doesn't support multi-input fields (like the Name field) yet. 😄

@spivurno
Copy link
Author

spivurno commented Oct 7, 2021

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