Skip to content

Abilities API: Add the core/user-create, core/user-update and core/user-delete abilities - #16074

Open
jorgefilipecosta wants to merge 2 commits into
WordPress:trunkfrom
jorgefilipecosta:add/user-write-abilities
Open

jorgefilipecosta wants to merge 2 commits into
WordPress:trunkfrom
jorgefilipecosta:add/user-write-abilities

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #10775, with the shape from WordPress/abilities#22.

This PR adds the core/user-create, core/user-update and core/user-delete abilities to the user category:

  • core/user-create takes { data, fields } and returns the new user. data.username and data.email are required. The password is generated and never returned: the new user gets the email with the link to set one.
  • core/user-update takes { where: { id }, data, fields }, changes only the attributes in data and returns the user.
  • core/user-delete takes { where: { id }, reassign } and returns { outcome }: deleted, or removed on multisite, where the user leaves the current site and the account stays in the network. reassign is required: the ID of the user who takes over the content, or false.
  • data accepts email, name, first_name, last_name, nickname, slug, description, url, locale and roles, plus username on create.
  • Create requires create_users, update a logged-in user and delete delete_users (remove_users on multisite). Each call then checks the capabilities of the user for the target user and for the roles it sets.
  • The output is the core/users-query item, with the same fields and the same checks per field.
  • core/users-query is registered as before: its fields and item schemas moved to private helpers that the new abilities share. In its tests, the helper that registers it now unregisters the four user abilities, and the run route tests get the cases of the three new abilities.

Creating a user:

const user = await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-create/run',
	method: 'POST',
	data: {
		input: {
			data: { username: 'anasilva', email: 'ana@example.org', name: 'Ana Silva', roles: [ 'author' ] },
		},
	},
} );
{
	"id": 2,
	"name": "Ana Silva",
	"link": "http://example.org/author/anasilva/",
	"slug": "anasilva",
	"avatar_urls": {
		"24": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=24&d=mm&r=g",
		"48": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=48&d=mm&r=g",
		"96": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=96&d=mm&r=g"
	}
}

Changing the role and the description, and asking for some fields:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-update/run',
	method: 'POST',
	data: {
		input: {
			where: { id: user.id },
			data: { roles: [ 'editor' ], description: 'Writes the release notes.' },
			fields: [ 'name', 'email', 'roles', 'description' ],
		},
	},
} );
{ "id": 2, "name": "Ana Silva", "description": "Writes the release notes.", "email": "ana@example.org", "roles": [ "editor" ] }

Deleting the user and giving their content to user 1 (use the ID of a user of your site). With reassign: false the content is deleted with the user:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-delete/run',
	method: 'POST',
	data: { input: { where: { id: user.id }, reassign: 1 } },
} );
{ "outcome": "deleted" }

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

Testing

Verify unit tests are passing:

npm run test:php -- --group 66720

Or run the examples above in the editor console as an administrator.

AI disclosure: the change and tests were drafted with AI assistance (Claude Code).

…re/user-delete` abilities.

Add three abilities to the `user` category:

- `core/user-create` creates a user from `data` and returns it. The password is generated and never returned: the new user receives the email with the link to set one.
- `core/user-update` updates the given attributes of the user matched by `where` and returns it.
- `core/user-delete` deletes the user matched by `where`, or on multisite removes the user from the current site. `reassign` is required: the ID of the user who takes over the content, or false.

The attributes that can be written are the email, name, first name, last name, nickname, slug, description, URL, locale and roles, plus the username on create. Each call checks the capabilities of the current user for the target user and for the roles it sets.

The `fields` and item schemas of `core/users-query` move to private helpers that the new abilities share. That ability is registered as before.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

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

Props jorgefilipecosta.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

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

  • 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.

Replace the `@ticket` placeholder in the tests of `core/user-create`, `core/user-update` and `core/user-delete` with 66720.

See #66720.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core abilities Core abilities related tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant