DEV Community

Cover image for Removing auto-complete in Gravity Forms without a plugin
SeanAUS120
SeanAUS120

Posted on

Removing auto-complete in Gravity Forms without a plugin

We are working on a site for the career coaches at A Path That Fits. They have a number of office locations around the USA including Berkeley, Los Angeles, Brooklyn & San Francisco. To handle the different pages and locations with different coaches at each place we use Gravity Forms to manage signups on the site.

GF is a great plugin but we recently noticed it has issues with autocomplete on some browsers, so we wanted to disable it completely to make it easier for users.

The way autocomplete works in different browsers varies. While the general operation of autocomplete is governed by HTML5 standards, there is considerable opportunity for interpretation. Although the user experience is largely the same across all browser makers' slightly varying solutions for activating autocomplete, the technical variations make it impossible to find a universally applicable method of turning off autocomplete.

According to GF docs, the autocomplete attribute can be added to any input, select, or textarea to modify the autocomplete behavior of those fields, according to the specification. Additionally, it says that using the off value should turn off autocomplete for that field. Many browsers support this, but not all of them.

Due to the various ways that browsers behave, our snippet quickly checks the User Agent to find out which browser is loading the form. The suitable autocomplete value is subsequently assigned for that specific browser. In all desktop and mobile browsers we tested, autocomplete was disabled.

Here is our working snippet:

add_filter( 'gform_form_tag', 'gform_form_tag_autocomplete', 11, 2 );
function gform_form_tag_autocomplete( $form_tag, $form ) {
    if ( is_admin() ) return $form_tag;
    if ( GFFormsModel::is_html5_enabled() ) {
        $form_tag = str_replace( '>', ' autocomplete="off">', $form_tag );
    }
    return $form_tag;
}
add_filter( 'gform_field_content', 'gform_form_input_autocomplete', 11, 5 ); 
function gform_form_input_autocomplete( $input, $field, $value, $lead_id, $form_id ) {
    if ( is_admin() ) return $input;
    if ( GFFormsModel::is_html5_enabled() ) {
        $input = preg_replace( '/<(input|textarea)/', '<${1} autocomplete="off" ', $input ); 
    }
    return $input;
}
Enter fullscreen mode Exit fullscreen mode

Get more insights at:
Brisbane Agency

Top comments (0)