Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active July 10, 2016 21:45
Show Gist options
  • Save joshuadavidnelson/5ef56023d04a18cc1924 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/5ef56023d04a18cc1924 to your computer and use it in GitHub Desktop.
Edit fields on Gravity Form
<?php
/**
* Change the way a field is rendered in Gravity Forms.
*
* This example shows how you would dynamically create options for a select field.
*
* @author Joshua David Nelson, [email protected]
* @link
*/
add_filter( 'gform_pre_render_1', 'gform_currency_field' ); // be sure to change the number "1" to the form ID, this is a dynamic filter
function gform_currency_field( $form ) {
// find the currency field and add the options
foreach( $form["fields"] as &$field ) {
if( $field['type'] === 'select' && strpos( $field['cssClass'], 'currency') !== true ) {
// The first, "select" option
$choices = array( array( 'text' => 'Select a Currency', 'value' => ' ' ) );
$currency_list = $this->currency_options();
// Check for results
if ( !empty( $currency_list ) ) {
foreach ( $currency_list as $key => $value ) {
// add users to select options
if( $key == 'USD' ) {
$choices[] = array(
'text' => $value,
'value' => $key,
'isSelected' => 1,
);
} else {
$choices[] = array(
'text' => $value,
'value' => $key,
);
}
}
}
$field['choices'] = $choices;
break;
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment