Forminator and Autocomplete
-
Hello,
I was wondering whether I can add HTML parameters to autocomplete certain fields in the form according to this article on Google Dev.Thank you!
-
Hello @mirackygmailcom
The current Autofill implementation found under the Behaviour tab is about fetching data from logged-in users.
https://premium.wpmudev.org/docs/wpmu-dev-plugins/forminator/#behavior-formsAbout autocomplete though, I’ve pinged our developers about it, in case there’s an easy workaround or if this should be added in a future release.
We’ll keep you posted here as soon as possible. ??
Thank you,
DimitrisHi Dimitris,
I believe this should not be difficult to implement as it’s basically just rendering parameters to the label HTML tag.Please, keep me posted in this thread.
Thank you!
JanHello @mirackygmailcom
I hope you’re doing well!
Our developers have created a code snippet that uses Forminator’s hooks to add “autocomplete” attribute to your form and fields.
Please download the snippet from here:
https://gist.github.com/wpmudev-sls/b7b3bc8d26ffa0b8533e69c15632e142.Once downloaded, please unzip it and upload the wpmudev-forminator-add-autocomplete-attr.php file to your site’s wp-content/mu-plugins directory. If the mu-plugins folder doesn’t exist, please feel free to create it.
If you need more help on installing this custom plugin please read our detailed guide here:
Installing WPMU DEV Plugins > Installing Mu-pluginsPlease do some changes in this code as mentioned in a comments’ sections. For example, in line 15 please add your Forminator form ID. In example below the 334 is a form ID:
// Add form id to apply below if( $form_id != '334' ){ return; }
Let us know how it went
Kind regards,
NastiaHi Nastia,
I installed the snippet and changed the form id in the PHP file. But I’m lost what I should do next. I clicked through Forminator setting on that particular form and I didn’t find any new available fields or anything I could work with.Can you please explain to me how I should proceed now to add these autocomplete fields to respective input fields on a given form?
Also, how do I add other forms to the PHP file to get the new options? Let’s say 334 and 335?
Thanks a lot for some more guidance, I appreciate it.
Best,
JanI checked the snippet and it works according to the article you linked to in your initial post: it adds a proper “autocomplete” attribute to input fields in defined forms.
It’s, however, by definition a different thing than the “autofill” feature built-in into Forminator (that you can enable for logged-in users in form’s “Behaviour” settings).
The one built-in let’s you define which fields on the form should have “autofill” enabled and where should they take the data from. That means that, for example, if you add a “name” field to the form you can set it to be automatically pre-filled with logged in user first name.
The “autocomplete” that you asked for by linking to that Google article is something different. It’s an attribute (part of WHATWG HTML standard) and it works entirely “browser-side”. It’s not able to get any data from e.g. user profile too as there’s no access/interaction with database and also it would be impossible for not logged-in users.
What it does, basically, is that if you have a set of input-type fields on the form and you have already filled-in this or other forms using standard “input” fields (and saved the data in browser) then without that attribute, if you e.g. fill in “name” field browser will check “types” of other fields (e.g. that one of them is credit card number and another one is address) and attempt to automatically fill that in based on data saved in Chrome by you in the e past, trying to match that credit card number an address with the name you used.
The code that we shared will not create any new fields in Forminator or any new options. It just injects “autocomplete” attribute to existing “input” fields, as described in the article.
The example in a shared code works for a field identified as “name-1” and sets it to be auto-filled with credit card user name (saved in browser). You can change the field by replacing “name-1” with another field ID as seen on form (in “Fields” section when you’re editing the form) in this line
if( $field['element_id'] == 'name-1' ){
Later in the code you can also change the “type” of autocomplete (see the article that you linked to for possible values):
$name = 'cc-name';
and you can also repeat that entire “if” block of code to add more fields. For example, let’s say that there’s a name field (name-2) and an e-mail field (email-3) and you want them both to be autofilled based browser-saved data, with the person full name and e-mail accordingly:
if( $field['element_id'] == 'name-2' ){ if( strpos( $html, $element ) ){ $name = 'name'; return str_replace( $element, $element . ' autocomplete="' . $name . '"', $html ); } } if( $field['element_id'] == 'email-3' ){ if( strpos( $html, $element ) ){ $name = 'email'; return str_replace( $element, $element . ' autocomplete="' . $name . '"', $html ); } }
Kind regards,
AdamHi Adam,
thank you very much. You gave me a 100% clarity on the difference between autofill that Forminator uses and autocomplete attribute.I implemented it as you described for name, e-mail and phone and it works perfectly.
if( $field['element_id'] == 'text-1' ){ if( strpos( $html, $element ) ){ $name = 'name'; return str_replace( $element, $element . ' autocomplete="' . $name . '"', $html ); } } if( $field['element_id'] == 'email-1' ){ if( strpos( $html, $element ) ){ $name = 'email'; return str_replace( $element, $element . ' autocomplete="' . $name . '"', $html ); } } if( $field['element_id'] == 'phone-1' ){ if( strpos( $html, $element ) ){ $name = 'tel'; return str_replace( $element, $element . ' autocomplete="' . $name . '"', $html ); } }
Thanks a lot and have a great day!
Jan-
This reply was modified 4 years, 9 months ago by
Jan Mi?acky.
-
This reply was modified 4 years, 9 months ago by
- The topic ‘Forminator and Autocomplete’ is closed to new replies.