😆Change date format contact form 7, Change placeholder, add attribute full (ok)

1. Thêm id và class

[text* Name id:name class:input_box placeholder "Name"]
<input type="text" onfocus="(this.type='date')" onblur="(this.type='text')"  name="FromDate" value="" class="wpcf7-form-control wpcf7-date wpcf7-validates-as-required wpcf7-validates-as-date" aria-required="true" aria-invalid="false" placeholder="Start Date">

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 agoarrow-up-rightViewed 21k times15

How to add a custom attribute in the field Contact Form 7arrow-up-right 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?

Sharearrow-up-rightImprove this questionarrow-up-rightFollowedited Jan 23, 2018 at 21:04arrow-up-rightRob's user avatararrow-up-rightRobarrow-up-right14.6k2828 gold badges4848 silver badges6464 bronze badgesasked Sep 18, 2017 at 7:59SVE's user avatararrow-up-rightSVEarrow-up-right1,50444 gold badges2828 silver badges5555 bronze badges

Add a commentarrow-up-right

6 Answers

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

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Dec 20, 2019 at 20:32arrow-up-rightanswered Sep 20, 2017 at 8:13Oleg Apanovich's user avatararrow-up-rightOleg Apanovicharrow-up-right64666 silver badges1414 bronze badges

Add a commentarrow-up-right5

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

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Sep 7, 2022 at 23:54arrow-up-rightanswered Dec 20, 2019 at 18:08Tofandel's user avatararrow-up-rightTofandelarrow-up-right2,81011 gold badge2727 silver badges4848 bronze badges

Show 5 more commentsarrow-up-right3

Multiple attributes can be added also. eg

Sharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Apr 16, 2018 at 20:30Victor Drover's user avatararrow-up-rightVictor Droverarrow-up-right3111 bronze badgeAdd a commentarrow-up-right1

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).

Sharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Oct 8, 2020 at 14:42Drif.io's user avatararrow-up-rightDrif.ioarrow-up-right3622 bronze badges

Add a commentarrow-up-right0

I propose a solution from the one given by Tofandel without JS (CF7) error and to authorize the use of an attribute without value:

Sharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Aug 18, 2021 at 6:30Killian Leroux's user avatararrow-up-rightKillian Lerouxarrow-up-right133 bronze badgesAdd a commentarrow-up-right0

I use this simple method for setting attribute

Last updated