How to stop auto-filling the password box on the WooCommerce Edit Account page

This code is designed to disable browser autofill for password fields on the WooCommerce “Edit Account” page for logged-in users. It does this by setting the autocomplete attribute of password fields to "new-password" using JavaScript.

Why?

A bad experience occurs when the browser auto-fills the password box on this page. The password box is supposed to remain blank unless the person wants to change their password. If a browser fills the password box, then WordPress expects them to enter their new password as well. If the person is not intending to change their password, then submitting the form shows an error. The user must explicitly delete the auto-filled password to save any changes.

This code disables auto-filling of the password box specifically on the Edit Account page. It checks whether the user is signed in, and if signed in, whether the user is on this specific page. If these conditions are met, then the autofill is disabled. This code should not impact any other page including the regular login page.

Code

PHP
// Hook a custom function into the wp_footer action,
// which runs just before the closing </body> tag
add_action( 'wp_footer', 'disable_password_autofill_woocommerce' );

/**
 * Disable browser password autofill on the WooCommerce
 * "Edit Account" page to improve privacy and prevent 
 * unwanted password suggestions.
 */
function disable_password_autofill_woocommerce() {
    // Only run this script if the user is logged in
    if ( ! is_user_logged_in() ) {
        return;
    }

    // Ensure we are on a WooCommerce account page
    // (e.g., /my-account/)
    if ( ! is_account_page() ) {
        return;
    }

    // Further ensure we are specifically on the 
    // "Edit Account" endpoint (e.g., /my-account/edit-account/)
    if ( ! is_wc_endpoint_url( 'edit-account' ) ) {
        return;
    }

    // Output inline JavaScript into the page footer
    ?>
    <script>
    // Wait until the DOM is fully loaded
    document.addEventListener( 'DOMContentLoaded', function () {
    
    // Select all password input fields within the 
    // WooCommerce Edit Account form
    const passwordFields = document.querySelectorAll(
    'form.woocommerce-EditAccountForm input[type="password"]'
     );

     // Loop through each password field found
     passwordFields.forEach( function ( field ) {
     
     // Set the autocomplete attribute to 'new-password' 
     // to prevent browser autofill suggestions
     field.setAttribute( 'autocomplete', 'new-password' );});
     });
    </script>
    <?php
}

How to use

Use a plugin such as Code Snippets to add this code to your site.

References

FunctionDescriptionDeveloper Docs

add_action()

Registers a callback function to a specific action hook.

add_action() – Function | Developer.WordPress.org

is_user_logged_in()

Checks whether the current visitor is a logged-in user.

is_user_logged_in() – Function | Developer.WordPress.org

is_account_page()

Returns true when on any WooCommerce “My Account” page.

Conditional Tags – WooCommerce Developer Docs

is_wc_endpoint_url()

Checks whether the current request URL matches a WooCommerce endpoint (e.g., edit-account).

WooCommerce Endpoints – WooCommerce Developer Docs

document.addEventListener('DOMContentLoaded', ...)

Runs JavaScript code after the DOM is fully loaded.

DOMContentLoaded – MDN Web Docs

querySelectorAll()

Selects all elements matching a CSS selector.

querySelectorAll() – MDN Web Docs

setAttribute()

Sets an attribute on an HTML element.

setAttribute() – MDN Web Docs

License

Feel free to remix this content for your website.

Licensed under CC BY 4.0

Leave a Reply