Last active
August 26, 2025 15:55
-
-
Save batonac/14772b1fe314764899d541bfbcb2dcca to your computer and use it in GitHub Desktop.
Add standard autocomplete attributes to Fluent Form address fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add standard autocomplete attributes to Fluent Form standard fields | |
| * to improve Chrome autofill recognition. | |
| * | |
| * @param array $field The field properties. | |
| * @param array $form The form properties. | |
| * @return array The modified field properties. | |
| */ | |
| add_filter( | |
| hook_name: "fluentform/rendering_field_data_address", | |
| callback: "add_fluent_autocomplete_attributes", | |
| priority: 999, | |
| accepted_args: 1 | |
| ); | |
| add_filter( | |
| hook_name: "fluentform/rendering_field_data_input_name", | |
| callback: "add_fluent_autocomplete_attributes", | |
| priority: 999, | |
| accepted_args: 1 | |
| ); | |
| add_filter( | |
| hook_name: "fluentform/rendering_field_data_input_email", | |
| callback: "add_fluent_autocomplete_attributes", | |
| priority: 999, | |
| accepted_args: 1 | |
| ); | |
| add_filter( | |
| hook_name: "fluentform/rendering_field_data_phone", | |
| callback: "add_fluent_autocomplete_attributes", | |
| priority: 999, | |
| accepted_args: 1 | |
| ); | |
| function add_fluent_autocomplete_attributes($field): mixed | |
| { | |
| // Define autocomplete mappings for different field types | |
| $autocomplete_mappings = [ | |
| "input_name" => [ | |
| "first_name" => "given-name", | |
| "middle_name" => "additional-name", | |
| "last_name" => "family-name", | |
| ], | |
| "input_email" => "email", | |
| "phone" => "tel", | |
| "address" => [ | |
| "address_line_1" => "address-line1", | |
| "address_line_2" => "address-line2", | |
| "city" => "address-level2", | |
| "state" => "address-level1", | |
| "zip" => "postal-code", | |
| "country" => "country", | |
| ], | |
| ]; | |
| // Determine field type from element or infer from context | |
| $fieldType = $field["element"] ?? null; | |
| // Handle complex fields with sub-fields (address, input_name) | |
| if (isset($field["fields"]) && is_array(value: $field["fields"])) { | |
| foreach ($field["fields"] as $fieldName => &$subField) { | |
| if (isset($autocomplete_mappings[$fieldType][$fieldName])) { | |
| $subField["attributes"]["autocomplete"] = | |
| $autocomplete_mappings[$fieldType][$fieldName]; | |
| } | |
| } | |
| } | |
| // Handle simple fields (input_email, phone) | |
| else { | |
| if ( | |
| isset($autocomplete_mappings[$fieldType]) && | |
| is_string(value: $autocomplete_mappings[$fieldType]) | |
| ) { | |
| $field["attributes"]["autocomplete"] = | |
| $autocomplete_mappings[$fieldType]; | |
| } | |
| } | |
| return $field; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment