Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matty0501/5d600450e8c939faa926e692b2572d91 to your computer and use it in GitHub Desktop.
Save matty0501/5d600450e8c939faa926e692b2572d91 to your computer and use it in GitHub Desktop.
<?php
/**
* Modified version of this snippet: https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-capitilize-submitted-data.php
*
* Capitializes all words in Single Line Text, Paragraph Text, Address and Name fields - only when the value is all UPPERCASE or all lowercase
*/
// Update "123" to the ID of the Form
add_action( 'gform_pre_submission_123', 'gw_capitalize_uppercase_lowercase_data' );
function gw_capitalize_uppercase_lowercase_data( $form ) {
$applicable_input_types = array( 'address', 'text', 'textarea', 'name' );
foreach ( $form['fields'] as $field ) {
$input_type = GFFormsModel::get_input_type( $field );
if ( ! in_array( $input_type, $applicable_input_types ) ) {
continue;
}
if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
foreach ( $field['inputs'] as $input ) {
$input_key = sprintf( 'input_%s', str_replace( '.', '_', $input['id'] ) );
if( strtolower( rgpost( $input_key ) ) == $_POST[ $input_key ] || strtoupper( rgpost( $input_key ) ) == $_POST[ $input_key ] ){
$_POST[ $input_key ] = ucwords( strtolower( rgpost( $input_key ) ) );
}
}
} else {
$input_key = sprintf( 'input_%s', $field['id'] );
if( strtolower( rgpost( $input_key ) ) == $_POST[ $input_key ] || strtoupper( rgpost( $input_key ) ) == $_POST[ $input_key ] ){
$_POST[ $input_key ] = ucwords( strtolower( rgpost( $input_key ) ) );
}
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment