Add spaces to CF7 field names

December 8th, 2016

Contact Form 7 does not permit spaces in field names. But we can add a WordPress filter function to add them. By convention, use an underscore “_” in field names where you want a space. For example “Team_Name”. Then add this filter to repace “_” with with a space so that this field is saved in CFDB as “Team Name”.

Add the following code in your theme’s functions.php or via Add Shortcodes Actions and Filters plugin.

1
2
3
4
5
6
7
8
9
10
11
function filter_add_spaces_to_field_names($formData) {
    foreach ($formData->posted_data as $key => $value) {
        $key2 = str_replace('_', ' ', $key);
        if ($key != $key2) {
            unset($formData->posted_data[$key]);
            $formData->posted_data[$key2] = $value;
        }
    }
    return $formData;
}
add_filter('cfdb_form_data', 'filter_add_spaces_to_field_names');

 
See also: Manipulating Data Before it is Saved

Comments are closed.  Go To Support Forum