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' );

	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'] );
			$_POST[ $input_key ] = ucwords( strtolower( rgpost( $input_key ) ) );
		}
	}

	return $form;
}

Comments

  1. Luc Mercx
    Luc Mercx July 2, 2024 at 7:36 am

    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?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff July 2, 2024 at 8:44 am

      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,

  2. April
    April June 12, 2024 at 5:11 pm

    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!

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 13, 2024 at 6:11 am

      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,

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.