😆Change date format contact form 7, Change placeholder, add attribute full (ok)
Last updated
Last updated
Sử dụng trực tiếp minlength:1 maxlength:10 dùng được nhưng sử dụng đoạn code sau lại không hoạt động tốt không biết có phải do bất đồng bộ gì đó không?
Asked 5 years, 6 months agoModified 7 months agoViewed 21k times15
How to add a custom attribute in the field Contact Form 7 without javascript ?
For example, there is such a field on the page:
Question: is it possible to set these custom attributes (data-attr
, data-msg
) of fields in the admin panel?
There are not any option for add custom attribute in admin. you need to done with js or any custom code. contactform7.com/text-fields – Ankur Bhadania Sep 18, 2017 at 8:32
Sorted by: Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 26
Find the name of your field.
If the name of your field name="text-21", like in my example, add this code to functions.php file.
Note, it will add those attributes to all forms elements where the name is text-21, if you want prevent it then give your form element some unique name [text* unique-name]
And change the code to
That filter add data-attr to everything, if field doesn't exists it prints "data-attr" ecc everywhere. How to fix? – Davide De Maestri Jan 17, 2018 at 10:57
It seems that you have forms with the same element names. Change field name to something unique. Please pay attention I edit my answer. – Oleg Apanovich Jan 23, 2018 at 21:04
1This one helped great, to add autocomplete="both" autocomplete="off"
to disable Chrome autofill dropdown for DATE selector field. Thanks! add_filter( 'wpcf7_form_elements', 'imp_wpcf7_form_elements' ); function imp_wpcf7_form_elements( $content ) { $str_pos = strpos( $content, 'name="date-from"' ); $content = substr_replace( $content, ' autocomplete="both" autocomplete="off" ', $str_pos, 0 ); $str_pos = strpos( $content, 'name="date-to"' ); $content = substr_replace( $content, ' autocomplete="both" autocomplete="off" ', $str_pos, 0 ); return $content; }
– Arnis Juraga Jul 9, 2019 at 17:42
Thanks Tofandel your remarks have sense I approved your answer editing, and it became better. – Oleg Apanovich Dec 20, 2019 at 20:21
Here is a generic solution that doesn't involve hardcoding the field name and the attributes
It works on all data attributes so you can use it like this
Since wpcf7 doesn't provide a way to hook into options directly the solutions uses a trick and temporary adds a uniquely generated class to the field that is then replaced in a later filter with the added attributes
If you need it to work with more than just data attributes you can whitelist some more attributes by replacing this line
to something like the following
Note: wpcf7_format_atts
will not output empty attributes, so make sure you give a value to your attributes
Cannot get it to work with CF7 v5.1.8. data-* stuff is simply ignored. Did you test your solution with CF7 v5.1.8? – DarkLeafyGreen May 15, 2020 at 18:07
1Yes, it still works, just make sure the attribute is right after the name and not after an attribute like placeholder "Blabla"
or contact form will not parse the field at all (regex issue, not related to my code). I see no reason the backward compatibility for this filter will ever be broken as it's pretty low level – Tofandel May 16, 2020 at 16:55
thanks! How would you provide a pattern like [0-9()#&+*-=.]+? I tried it with your solution and the output could not be created. – DarkLeafyGreen May 20, 2020 at 9:58
$tag['name'] = $id = uniqid(); this part is wrong as it make the field id for the email part change to uniq id of the form isntade. So it should be - $tag['name'] = $id = $name; – Soykot Jun 29, 2020 at 21:29
1Note that this solution breaks CF7's validation: invalid required input fields using custom data-* attributes won't be given the wpcf7-not-valid
CSS class on submission. – cabrerahector Jul 2, 2020 at 20:10
Multiple attributes can be added also. eg
Extending on from Tofandel's solution, for the benefit of those who got 99% of the way there, but suffered validation issues - I've resolved that in my case and would like to offer an extended solution that gets as far as Tofandel's (putting the attribute into the form proper) but also successfully validates on submission.
Rather than changing the tag ID to a random value only to replace it with the "real" value later, we just reference the real value in the first place, replacing the relevant part of the content in the wpcf7_form_elements filter (in my case, autocomplete, but as Tofandel's example shows, this can be extended to any data attribute you'd like).
Just FYI, I didn't do it like this because I needed to support multiple contact forms on a same page, and this will mix up the attributes between them – Tofandel Sep 7, 2022 at 23:11
I updated my answer to add a unique class and use that instead of changing the tag name, which should take care of both problems – Tofandel Sep 7, 2022 at 23:40
I propose a solution from the one given by Tofandel without JS (CF7) error and to authorize the use of an attribute without value:
I use this simple method for setting attribute
ShareImprove this questionFollowedited Jan 23, 2018 at 21:04Rob14.6k2828 gold badges4848 silver badges6464 bronze badgesasked Sep 18, 2017 at 7:59SVE1,50444 gold badges2828 silver badges5555 bronze badges
ShareImprove this answerFollowedited Dec 20, 2019 at 20:32answered Sep 20, 2017 at 8:13Oleg Apanovich64666 silver badges1414 bronze badges
ShareImprove this answerFollowedited Sep 7, 2022 at 23:54answered Dec 20, 2019 at 18:08Tofandel2,81011 gold badge2727 silver badges4848 bronze badges
ShareImprove this answerFollowanswered Apr 16, 2018 at 20:30Victor Drover3111 bronze badgeAdd a comment1
ShareImprove this answerFollowanswered Oct 8, 2020 at 14:42Drif.io3622 bronze badges
ShareImprove this answerFollowanswered Aug 18, 2021 at 6:30Killian Leroux133 bronze badgesAdd a comment0