Adding autocomplete attributes
-
Is there any way to add autocomplete attributes to a form field?
Thanks!
-
Hey @treylok
This can be achieved with a custom snippet for the time being until we add this feature.
Place this snippet in your child theme’s functions.php file and modify the IDs accordingly.add_filter('mf_field_display_args', 'mf_custom_field_display_attributes', 10, 1);
function mf_custom_field_display_attributes($params) {
$traget_form_id = 80;
if ( strpos($params['id'], "mform_{$traget_form_id}_mfield_") !== 0 ) {
return $params;
}
$prefix = "mform_{$traget_form_id}_mfield_";
$firstname_field_id = 1;
$lastname_field_id = 2;
$email_field_id = 3;
switch ($params['id']) {
case "{$prefix}{$firstname_field_id}":
$params['attributes']['autocomplete'] = 'given-name';
break;
case "{$prefix}{$lastname_field_id}":
$params['attributes']['autocomplete'] = 'family-name';
break;
case "{$prefix}{$email_field_id}":
$params['attributes']['autocomplete'] = 'off';
break;
default:
break;
}
return $params;
}Let me know if you have any additional questions.
Thanks so much, it is working for a one line fields, but I am having some issues with the email field. I am also getting some errors in the console. The errors are with or without the snippet. It appears the id attribute is being applied to the sub label field for email and there is an [email] being added to the end of the name field. Not sure if this is intended or not. Below are some screen shots.
Hello @treylok
Since email field is a compound field ( consisting of more than one input ), it has a different rendering approach. In this case, we can use another hook that allows filtering all types of fields.
The following code should fix both problems for you
add_filter('mf_input_args', 'mf_custom_input_attributes', 10, 1);
function mf_custom_input_attributes($args) {
$traget_form_id = 81;
if ( !isset($args['attributes']['name']) || strpos($args['attributes']['name'], "mform_{$traget_form_id}_mfield_") !== 0 ) {
return $$args;
}
$prefix = "mform_{$traget_form_id}_mfield_";
$email_field_id = 7;
$email_suffix = $email_field_id . '[email]';
$confirm_email_suffix = $email_field_id . '[email_confirmation]';
switch ($args['attributes']['name']) {
case "{$prefix}{$email_field_id}":
$args['attributes']['id'] = $prefix . $email_field_id . '_email';
$args['content'] = str_replace('for="' . $prefix . $email_suffix . '"', 'for="' . $prefix . $email_field_id . '_email"', $args['content']);
$args['content'] = str_replace('for="' . $prefix . $confirm_email_suffix . '"', 'for="' . $prefix . $email_field_id . '_email_confirmation"', $args['content']);
break;
case "{$prefix}{$email_suffix}":
$args['content'] = str_replace('/>', ' autocomplete="email" id="' . $prefix . $email_field_id . '_email"/>', $args['content']);
$args['attributes']['id'] = $prefix . $email_field_id . '_email';
break;
case "{$prefix}{$confirm_email_suffix}":
$args['content'] = str_replace('/>', ' autocomplete="off" id="' . $prefix . $email_field_id . '_email_confirmation"/>', $args['content']);
$args['attributes']['id'] = $prefix . $email_field_id . '_email_confirmation';
break;
default:
break;
}
return $args;
}Make sure to change the form ID and the field ID and it will work as expected.
Note that the [email] part is autogenerated for compound fields, so it is intended.It works, thanks!
- You must be logged in to reply to this topic.