Capitalize Submitted Data
Capitializes all words in Single Line Text, Paragraph Text, Address and Name fields.
Instructions
Code
Filename: gw-capitilize-submitted-data.php
<?php
/**
* Gravity Wiz // Gravity Forms // Capitalize Submitted Data
* https://gravitywiz.com/
*
* Instruction Video: https://www.loom.com/share/dff6d63f5a9c44938fdc0506ce5d4965
*
* Capitializes all words in Single Line Text, Paragraph Text, Address and Name fields.
*/
// Update "123" to the ID of the Form
add_action( 'gform_pre_submission_123', 'gw_capitalize_submitted_data' );
function gw_capitalize_submitted_data( $form ) {
$applicable_input_types = array( 'address', 'text', 'textarea', 'name', 'list' );
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'] ) );
$_POST[ $input_key ] = ucwords( strtolower( rgpost( $input_key ) ) );
}
} else {
$input_key = sprintf( 'input_%s', $field['id'] );
if ( $field->type == 'list' ) {
$_POST[ $input_key ] = array_map( function( $value ) {
return ucwords( strtolower( $value ) );
}, $_POST[ $input_key ] );
} else {
$_POST[ $input_key ] = ucwords( strtolower( rgpost( $input_key ) ) );
}
}
}
return $form;
}
Hi There, this works like a charm. I do have an additional question. In The Netherlands we use three fields for a name: first name, last name and also infix. But in The Netherlands only the first and last name are written capitalized but the infix not. How can I tweak this? Is this possible?
Hi Luc,
This should be possible with some customization to the snippet. I’m not entirely sure if you’re using three different fields or the Name field with three sub-fields. I’ll email you to confirm your setup so we can tweak the snippet for your use case.
Best,
This works perfectly! It should be called Propercase though, since it makes all of the characters in a word lowercase and only capitalizes the first letter. Which is exactly what I was looking for! It even capitalizes words in addresses and it doesn’t capitalize the email address.
Genius! Thank you!
Hi April,
You’re right, this is proper case and that is actually how the snippet is supposed to work, make the first letter of every word in a field uppercase. Glad to know it was helpful to you.
Best,