As of WPCF7 version 4.9, adapting the answer above so as to allow for error messaging to not be lost:
First of all, in the editor in the CMS, wrap your input field and any other elements that you want grouped, for example:
<span class="wpcf7-form-control-wrap your-name">{field codes, etc, here}</span>
Note that you'll need to use the class "wpcf7-form-control-wrap" and a class that matches your field name.
Next, use this regex code in your functions.php
. It may need adapting for your specific needs
add_filter('wpcf7_form_elements', function($content) {
preg_match_all('/<(span).*?class="\s*(?:.*\s)?wpcf7-form-control-wrap(?:\s[^"]+)?\s*"[^\>]*>(.*)<\/\1>/i', $content,$matches);
foreach($matches[2] as $match):
$content = str_replace(trim($match),trim(preg_replace('/<(span).*?class="\s*(?:.*\s)?wpcf7-form-control-wrap(?:\s[^"]+)?\s*"[^\>]*>(.*)<\/\1>/i', '\2', $match)),$content);
endforeach;
return $content;
});
This will strip the span tag around the input field while leaving your new span tag intact. The effect is to essentially move the span tag from around only the input field, to being around whatever elements you wish to wrap.
The reason for this is that the code for form submission is unfortunately very hard coded. In order to have complete freedom over the structure of your HTML you would need to either:
Change the code in rest-api.php around line 295 to change the "into" value to something less specific. Naturally this isn't a recommended route though the one that gives you complete freedom to structure your content as you wish. It'll be overwritten with plugin updates.
foreach ( (array) $result['invalid_fields'] as $name => $field ) {
$invalid_fields[] = array(
'into' => 'span.wpcf7-form-control-wrap.'
. sanitize_html_class( $name ),
'message' => $field['reason'],
'idref' => $field['idref'],
);
}
Tap into the wpcf7:invalid event and run your own validation code on the result. Needless to say this is duplicating a lot of the work the plugin already does for you, when accepting (for now) to use a span tag with the "wpcf7-form-control-wrap" class in the manner described above retains all functionality of the plugin while undoing one of the most annoying hard codings of the plugin.