Skip to content

Abilities API: Add the core/media-query, core/media-upload, core/media-update and core/media-delete abilities - #16138

Draft
jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/media-abilities
Draft

jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/media-abilities

Conversation

@jorgefilipecosta

Copy link
Copy Markdown
Member

Shape from WordPress/abilities#22. Same approach as #16028.

This PR adds the core/media-query, core/media-upload, core/media-update and core/media-delete abilities to the content category, for attachments:

  • core/media-query takes { where, fields, pagination } and returns { items, pagination } with the totals. ids, slugs, status, media_type, mime_type, post and author filter the attachments.
  • core/media-upload takes { data, fields } and returns the new attachment. data.file is required: { url } for an image to download, or { base64, filename } for a file of any type the user may upload.
  • core/media-update takes { where: { id }, data, fields }, changes only the attributes in data and returns the attachment. post attaches it to a post, and post: 0 detaches it.
  • core/media-delete takes { where: { id }, permanent } and returns { outcome }. With permanent: true the attachment and its files are deleted. Without it the attachment goes to the trash, and on a site that does not set MEDIA_TRASH the answer is a media_trash_not_supported error.
  • data accepts title, caption, description, alt_text, slug, date, author and post.
  • The abilities require a logged-in user, and each call checks the capabilities of the user for the attachment and for the values it sets.

Outside the new WP_Abilities_Media class:

  • wp_register_core_abilities() loads the class and registers the abilities.

Uploading an image from a URL:

const media = await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/media-upload/run',
	method: 'POST',
	data: {
		input: {
			data: {
				file: { url: 'https://s.w.org/style/images/about/WordPress-logotype-standard.png' },
				title: 'WordPress logotype',
				alt_text: 'The WordPress logotype.',
			},
		},
	},
} );
{
	"id": 8,
	"status": "inherit",
	"date": { "site": "2026-11-20T18:01:55+00:00", "gmt": "2026-11-20T18:01:55+00:00" },
	"slug": "wordpress-logotype",
	"title": { "rendered": "WordPress logotype" },
	"media_type": "image",
	"mime_type": "image/png",
	"source_url": "http://localhost:8951/wp-content/uploads/2026/11/WordPress-logotype-standard.png"
}

Changing its slug and caption, and asking for some fields:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/media-update/run',
	method: 'POST',
	data: {
		input: {
			where: { id: media.id },
			data: { slug: 'logotype', caption: 'The logotype on a light background.' },
			fields: [ 'slug', 'title', 'caption', 'alt_text' ],
		},
	},
} );
{
	"id": 8,
	"slug": "logotype",
	"title": { "raw": "WordPress logotype", "rendered": "WordPress logotype" },
	"caption": {
		"raw": "The logotype on a light background.",
		"rendered": "<p>The logotype on a light background.</p>\n"
	},
	"alt_text": "The WordPress logotype."
}

Querying the images, two per page:

await wp.apiFetch( {
	path: wp.url.addQueryArgs( '/wp-abilities/v1/abilities/core/media-query/run', {
		input: {
			where: { media_type: [ 'image' ] },
			fields: [ 'title.rendered', 'mime_type', 'media_details.width', 'media_details.height' ],
			pagination: { per_page: 2 },
		},
	} ),
} );
{
	"items": [
		{
			"id": 10,
			"title": { "rendered": "WordPress simplified logotype" },
			"mime_type": "image/png",
			"media_details": { "width": 500, "height": 500 }
		},
		{
			"id": 9,
			"title": { "rendered": "WordPress logotype with the mark" },
			"mime_type": "image/png",
			"media_details": { "width": 1000, "height": 1000 }
		}
	],
	"pagination": { "total": 3, "total_pages": 2 }
}

Deleting it permanently. Without permanent: true the answer on this site is a media_trash_not_supported error:

await wp.apiFetch( {
	path: wp.url.addQueryArgs( '/wp-abilities/v1/abilities/core/media-delete/run', {
		input: { where: { id: media.id }, permanent: true },
	} ),
	method: 'DELETE',
} );
{ "outcome": "deleted" }

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

Testing

Verify unit tests are passing:

npm run test:php -- --group 66719

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

…/media-update` and `core/media-delete` abilities.

Adds four abilities for attachments.

`core/media-query` returns a page of attachments with the totals, filtered by IDs, slugs, status, media type, MIME type, parent post or author. `core/media-upload` stores a file, sent as base64 data or as the URL of an image to download, and creates its attachment. `core/media-update` changes the attributes of an attachment, and `core/media-delete` trashes it or deletes it permanently together with its files. The upload and update abilities return the attachment in the same form as the query.

The abilities require a logged-in user, and each call checks the capabilities of the user for the attachment and for the values it sets.

See #66719.
@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.

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.

1 participant