Modify form data before it is processed

In this guide we will create a PHP function that will alter two of the form fields before the form is processed. We will modify the “First name” and “Last name” fields of the form and force the text to be uppercase.

Step 1

Get the unique ID of your form, see Finding the form ID.

Step 2

Get the unique IDs of the “First name” and “Last name” fields of the form, see Finding the unique element ID.

Step 3

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
456
7
8
91011
12
13
function my_alter_form_values($form)
{
    // Set the first name value to uppercase
    if (isset($_POST['iphorm_1_1'])) {        $_POST['iphorm_1_1'] = strtoupper($_POST['iphorm_1_1']);    }
 
    // Set the last name value to uppercase
    if (isset($_POST['iphorm_1_2'])) {        $_POST['iphorm_1_2'] = strtoupper($_POST['iphorm_1_2']);    }
}
add_action('iphorm_pre_process_1', 'my_alter_form_values');
function my_alter_form_values($form)
{
    // Set the first name value to uppercase
    if (isset($_POST['iphorm_1_1'])) {
        $_POST['iphorm_1_1'] = strtoupper($_POST['iphorm_1_1']);
    }

    // Set the last name value to uppercase
    if (isset($_POST['iphorm_1_2'])) {
        $_POST['iphorm_1_2'] = strtoupper($_POST['iphorm_1_2']);
    }
}
add_action('iphorm_pre_process_1', 'my_alter_form_values');

Modify the highlighted lines to suit your form setup.

  • On lines 4 and 5, change iphorm_1_1 to the unique ID of the “First name” element
  • On lines 9 and 10, iphorm_1_2 to the unique ID of the “Last name” element
  • On line 13, change the number 1 to the form unique ID

Step 4

Save the file and upload it to your web server.

Still having trouble? Head over to the forums.

Forums