Your cart is currently empty!
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.
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.
// 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
}
Use a plugin such as Code Snippets to add this code to your site.
Function | Description | Developer Docs |
---|---|---|
| Registers a callback function to a specific action hook. | add_action() – Function | Developer.WordPress.org |
| Checks whether the current visitor is a logged-in user. | is_user_logged_in() – Function | Developer.WordPress.org |
| Returns true when on any WooCommerce “My Account” page. | Conditional Tags – WooCommerce Developer Docs |
| Checks whether the current request URL matches a WooCommerce endpoint (e.g., edit-account). | WooCommerce Endpoints – WooCommerce Developer Docs |
| Runs JavaScript code after the DOM is fully loaded. | DOMContentLoaded – MDN Web Docs |
| Selects all elements matching a CSS selector. | querySelectorAll() – MDN Web Docs |
| Sets an attribute on an HTML element. | setAttribute() – MDN Web Docs |
Having trouble?
We complete tasks for you. Our goal is to offload your technical labor so you can focus on business and innovation.
$60
per hour
Fixed rate for all types of tasks.
No monthly charges.
Pay by the minute.
Leave a Reply
You must be logged in to post a comment.