I use Contact Form 7 in my WordPress theme.
It is currently returning span
and input
:
<span class="wpcf7-form-control-wrap name">
<input type="text" name="name" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required form-control" id="name">
</span>
But I need only input
:
<input type="text" name="name" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required form-control" id="name">
How can I remove the span
wrapper?
I tried similar stuff the other day and many people mentioned, that Regex is not the proper way to alter HTML/remove tags etc and it sounds logic.
So i went for DOMDocument and came up with following solution:
EDIT: I now also added some html/text to my form, and the first heading element was not wrapping correctly after i used the DOMDocument class. It started in the first line and ended at the very end of the form. so i wrapped my entire form in a div, what made the markup return properly again.
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:
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 needsThis 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.
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.
You can remove the span wrapper using jQuery:
It will remove input's parent element, so in this case it will remove the span. Please note that after removing the span, some Contact Form 7 features may not work correctly. For example validation may not work.
I faced the same problem and finally ending by using the
wpcf7_form_elements
filter to remove the<span>
tag with a regex. You can for example copy-paste this code in yourfunctions.php
file. Here I pass an an anonymous function as a callback, so be sure to have PHP >= 5.3.