Repository navigation
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
Draft
jorgefilipecosta wants to merge 1 commit into
jorgefilipecosta wants to merge 1 commit into
Conversation
…/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.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Shape from WordPress/abilities#22. Same approach as #16028.
This PR adds the
core/media-query,core/media-upload,core/media-updateandcore/media-deleteabilities to thecontentcategory, for attachments:core/media-querytakes{ where, fields, pagination }and returns{ items, pagination }with the totals.ids,slugs,status,media_type,mime_type,postandauthorfilter the attachments.core/media-uploadtakes{ data, fields }and returns the new attachment.data.fileis required:{ url }for an image to download, or{ base64, filename }for a file of any type the user may upload.core/media-updatetakes{ where: { id }, data, fields }, changes only the attributes indataand returns the attachment.postattaches it to a post, andpost: 0detaches it.core/media-deletetakes{ where: { id }, permanent }and returns{ outcome }. Withpermanent: truethe attachment and its files are deleted. Without it the attachment goes to the trash, and on a site that does not setMEDIA_TRASHthe answer is amedia_trash_not_supportederror.dataacceptstitle,caption,description,alt_text,slug,date,authorandpost.Outside the new
WP_Abilities_Mediaclass:wp_register_core_abilities()loads the class and registers the abilities.Uploading an image from a URL:
{ "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:
{ "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:
{ "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: truethe answer on this site is amedia_trash_not_supportederror:{ "outcome": "deleted" }Trac ticket: https://core.trac.wordpress.org/ticket/66719
Testing
Verify unit tests are passing:
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).