From 877bf3de798eaf1884a6eb8cbf12997b5b47b71f Mon Sep 17 00:00:00 2001 From: Tanin Ahmed Date: Sat, 30 Aug 2025 01:26:08 +0600 Subject: [PATCH 1/4] Users: Fix Generate Password button requiring two clicks to update the password field on the Add New User screen. The password was previously set before the AJAX response returned, resulting in the first click not updating the password field. This change moves the `generatePassword()` call inside the AJAX callback to ensure the password is always updated immediately. Props taninahmed. Fixes #63897. --- src/js/_enqueues/admin/user-profile.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/js/_enqueues/admin/user-profile.js b/src/js/_enqueues/admin/user-profile.js index ce680ef4c4298..b08a6bcaf4b0e 100644 --- a/src/js/_enqueues/admin/user-profile.js +++ b/src/js/_enqueues/admin/user-profile.js @@ -255,9 +255,6 @@ $pass1.attr( 'disabled', false ); $pass2.attr( 'disabled', false ); - // Set the password to the generated value. - generatePassword(); - // Show generated password in plaintext by default. resetToggle ( false ); @@ -265,6 +262,8 @@ wp.ajax.post( 'generate-password' ) .done( function( data ) { $pass1.data( 'pw', data ); + // Set the password to the generated value. + generatePassword(); } ); } ); From 17f7e959b1055ed3a5a58c5c34b11b125ac1f7a4 Mon Sep 17 00:00:00 2001 From: Tanin Ahmed Date: Sun, 31 Aug 2025 23:28:44 +0600 Subject: [PATCH 2/4] Move network password generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the block at lines 264–268 to line 63, directly under the generatePassword call. This ensures that: 1. The password field is always initialized with the value set in $pass1.data('pw'), which comes from PHP and is set at line 32. 2. Any network request to fetch a new password happens after the initial password is set, so there is no delay in showing a password on page load. 3. When the user clicks the "Generate Password" button, a new password is fetched and stored in data-pw, ensuring the latest password is always available. --- src/js/_enqueues/admin/user-profile.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/js/_enqueues/admin/user-profile.js b/src/js/_enqueues/admin/user-profile.js index b08a6bcaf4b0e..e84d7151a4aba 100644 --- a/src/js/_enqueues/admin/user-profile.js +++ b/src/js/_enqueues/admin/user-profile.js @@ -60,6 +60,12 @@ if ( 'mailserver_pass' !== $pass1.prop('id' ) && ! $('#weblog_title').length ) { $( $pass1 ).trigger( 'focus' ); } + + // Generate the next password and cache. + wp.ajax.post( 'generate-password' ) + .done( function( data ) { + $pass1.data( 'pw', data ); + } ); } function bindPass1() { @@ -255,16 +261,11 @@ $pass1.attr( 'disabled', false ); $pass2.attr( 'disabled', false ); + // Set the password to the generated value. + generatePassword(); + // Show generated password in plaintext by default. resetToggle ( false ); - - // Generate the next password and cache. - wp.ajax.post( 'generate-password' ) - .done( function( data ) { - $pass1.data( 'pw', data ); - // Set the password to the generated value. - generatePassword(); - } ); } ); $cancelButton = $pass1Row.find( 'button.wp-cancel-pw' ); From eb420bae17eb8d7736500cd0e41fda902978061f Mon Sep 17 00:00:00 2001 From: Tanin Ahmed Date: Sun, 31 Aug 2025 23:29:24 +0600 Subject: [PATCH 3/4] Remove redundant code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The block at lines 533–542 that triggered the "Generate Password" button click on page load is no longer needed. Passwords are already automatically filled via the bindPass1 function, which calls generatePassword() on initialization. --- src/js/_enqueues/admin/user-profile.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/js/_enqueues/admin/user-profile.js b/src/js/_enqueues/admin/user-profile.js index e84d7151a4aba..c6711ad46e8b8 100644 --- a/src/js/_enqueues/admin/user-profile.js +++ b/src/js/_enqueues/admin/user-profile.js @@ -529,16 +529,4 @@ return __( 'The changes you made will be lost if you navigate away from this page.' ); } }); - - /* - * We need to generate a password as soon as the Reset Password page is loaded, - * to avoid double clicking the button to retrieve the first generated password. - * See ticket #39638. - */ - $( function() { - if ( $( '.reset-pass-submit' ).length ) { - $( '.reset-pass-submit button.wp-generate-pw' ).trigger( 'click' ); - } - }); - })(jQuery); From a0a7f93cd18868f0ea8d0d8438631aac662e7367 Mon Sep 17 00:00:00 2001 From: Tanin Ahmed Date: Mon, 1 Sep 2025 23:53:20 +0600 Subject: [PATCH 4/4] Prevent unnecessary AJAX requests for password generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wrap the `generate-password` AJAX call in a conditional check to ensure it only runs when the `.wp-generate-pw` element is present. This avoids unnecessary requests on pages such as: - Settings → Writing - Installer "Information needed" screen (which could result in 404 errors) --- src/js/_enqueues/admin/user-profile.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/js/_enqueues/admin/user-profile.js b/src/js/_enqueues/admin/user-profile.js index c6711ad46e8b8..8506e38f5a390 100644 --- a/src/js/_enqueues/admin/user-profile.js +++ b/src/js/_enqueues/admin/user-profile.js @@ -61,11 +61,13 @@ $( $pass1 ).trigger( 'focus' ); } - // Generate the next password and cache. - wp.ajax.post( 'generate-password' ) - .done( function( data ) { - $pass1.data( 'pw', data ); - } ); + if($( '.wp-generate-pw').length) { + // Generate the next password and cache. + wp.ajax.post( 'generate-password' ) + .done( function( data ) { + $pass1.data( 'pw', data ); + } ); + } } function bindPass1() {