Convert specific form field to uppercase in Fluent Forms

tdrayson

A user in the Fluent Form Facebook group was looking for a way ensure an input from a user was always uppercase. Luckily we can hook into Fluent Forms and manually modify the passed in inputs before they are saved to the database.

Add the code below to functions.php or code snippet plugin.

PHP

        /**
 * Convert specific form field to uppercase.
 *
 * This function converts the 'subject' field of form with ID 12 to uppercase.
 *
 * @since 25/07/2024
 * @author Taylor Drayson
 *
 * @param array $formData     The form data submitted.
 * @param int   $formId       The form ID.
 * @param array $inputConfigs The input configurations.
 * @return array Modified form data.
 */
function tct_convert_field_to_uppercase( $formData, $formId, $inputConfigs ) {
    $target_form = 12;
    $target_field = 'subject';

    if ( $formId !== $target_form ) {
        return $formData;
    }

    if ( ! isset( $formData[ $target_field ] ) ) {
        return $formData;
    }

    $formData[ $target_field ] = strtoupper( $formData[ $target_field ] );

    return $formData;
}
add_filter( 'fluentform/insert_response_data', 'tct_convert_field_to_uppercase', 10, 3 );
      
Copy

Breakdown

In this snippet, we are checking to see if we match the $target_form and the $target_field that we need.

We return early if either of these checks fail. If they pass we use the built in strtoupper PHP function to modify the input field, which is then returned in the filter.

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise