Skip to content

set password action on user insert and update#9244

Closed
nimesh-xecurify wants to merge 2 commits intoWordPress:trunkfrom
nimesh-xecurify:22114-password-update-hook
Closed

set password action on user insert and update#9244
nimesh-xecurify wants to merge 2 commits intoWordPress:trunkfrom
nimesh-xecurify:22114-password-update-hook

Conversation

@nimesh-xecurify
Copy link

Fires a wp_set_password action whenever a user is created or updated.

Trac ticket: https://core.trac.wordpress.org/ticket/22114

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props nimeshatxecurify.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@nimesh-xecurify nimesh-xecurify marked this pull request as draft July 18, 2025 11:23
@nimesh-xecurify nimesh-xecurify marked this pull request as ready for review July 18, 2025 11:25
Comment on lines +2417 to +2420
// Assert initial password action (creation)
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during user creation.' );
$mock_action->reset(); // Reset for update test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Assert initial password action (creation)
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during user creation.' );
$mock_action->reset(); // Reset for update test

I think this is covered by the previous test case.

Copy link
Contributor

@dream-encode dream-encode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor tweaks, working on a commit now.

Comment on lines +2403 to +2430
public function test_set_password_action_on_user_update() {
$mock_action = new MockAction();
add_action( 'wp_set_password', array( $mock_action, 'action' ), 10, 3 );

// Step 1: Create a user
$user_id = $this->factory()->user->create(
array(
'role' => 'subscriber',
'user_login' => 'testuser_update',
'user_email' => 'testuser_update@example.com',
'user_pass' => 'initialpassword',
)
);

// Assert initial password action (creation)
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during user creation.' );
$mock_action->reset(); // Reset for update test

// Step 2: Update the password via wp_set_password
$updated_password = 'newpassword123';
wp_set_password( $updated_password, $user_id );

// Assert action triggered
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during password update.' );
$args = $mock_action->get_args();
$this->assertSame( $updated_password, $args[0][0], 'Invalid password in wp_set_password action.' );
$this->assertSame( $user_id, $args[0][1], 'Invalid user ID in wp_set_password action.' );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function test_set_password_action_on_user_update() {
$mock_action = new MockAction();
add_action( 'wp_set_password', array( $mock_action, 'action' ), 10, 3 );
// Step 1: Create a user
$user_id = $this->factory()->user->create(
array(
'role' => 'subscriber',
'user_login' => 'testuser_update',
'user_email' => 'testuser_update@example.com',
'user_pass' => 'initialpassword',
)
);
// Assert initial password action (creation)
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during user creation.' );
$mock_action->reset(); // Reset for update test
// Step 2: Update the password via wp_set_password
$updated_password = 'newpassword123';
wp_set_password( $updated_password, $user_id );
// Assert action triggered
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during password update.' );
$args = $mock_action->get_args();
$this->assertSame( $updated_password, $args[0][0], 'Invalid password in wp_set_password action.' );
$this->assertSame( $user_id, $args[0][1], 'Invalid user ID in wp_set_password action.' );
}

Actually, this whole test is a dupe of Tests_Auth::test_wp_set_password_action()

Comment on lines +2377 to +2388

// Step 2: Update the user password explicitly via `wp_set_password`.
$updated_password = 'updatedpassword123';
wp_set_password( $updated_password, $user_id );

// Assert that `wp_set_password` was triggered once during the password update.
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during password update.' );
$args = $mock_action->get_args();
$this->assertSame( $updated_password, $args[0][0], 'Wrong password argument in action when updating user.' );
$this->assertSame( $user_id, $args[0][1], 'Wrong user ID in action when updating user.' );

$mock_action->reset(); // Reset mock for the final scenario.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Step 2: Update the user password explicitly via `wp_set_password`.
$updated_password = 'updatedpassword123';
wp_set_password( $updated_password, $user_id );
// Assert that `wp_set_password` was triggered once during the password update.
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during password update.' );
$args = $mock_action->get_args();
$this->assertSame( $updated_password, $args[0][0], 'Wrong password argument in action when updating user.' );
$this->assertSame( $user_id, $args[0][1], 'Wrong user ID in action when updating user.' );
$mock_action->reset(); // Reset mock for the final scenario.

Same here. Dupe code.

@github-actions
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60634
GitHub commit: 9098796

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Aug 13, 2025
@nimesh-xecurify nimesh-xecurify deleted the 22114-password-update-hook branch February 19, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants