Skip to content

Attachments/media endpoint: Add an embeddable post as a link in attachments REST API response - #10027

Closed
andrewserong wants to merge 5 commits into
WordPress:trunkfrom
andrewserong:add/post-to-attachment-links-in-rest-api-response
Closed

andrewserong wants to merge 5 commits into
WordPress:trunkfrom
andrewserong:add/post-to-attachment-links-in-rest-api-response

Conversation

@andrewserong

@andrewserong andrewserong commented Sep 24, 2025 •

Copy link
Copy Markdown
Contributor

In the attachments controller (i.e. for the media endpoint) add post to the links in the API response, that provides a link to the API call to get the linked post — i.e. the parent, or the post that the media item is attached to.

The purpose is to make it easier to fetch linked posts or pages that media items are attached to, via the REST API. This is part of some of the work I've been exploring for the new media library (proposed in WordPress/gutenberg#55238). When listing (or providing UI to make changes to) the "uploaded to" field (or "attached to" or whatever it should be called), it'll be easier to do so if we can embed the post in the API response for media items.

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

Testing instructions

If you'd like to test this from within the block editor:

  1. Open up the media library and upload a couple of images.
  2. From the list view within the media library, click Attach under the Uploaded To column and attach one of the media items to a post
  3. Make a note of the ids of your two images (i.e. grab them from the url)
  4. Open the block editor, and try running the following from your browser console (note you might have to run these commands twice as the first call will return undefined as the request hasn't been resolved yet):
// Replace 27 with the id of an image that is attached to a post — you should see `post` in the list of `_links`
wp.data.select( 'core' ).getEntityRecord( 'postType', 'attachment', 27 );
// Again, replace 27 with the id of an image that is attached to a post — in this call, you should see the post under `_embedded` in the response
wp.data.select( 'core' ).getEntityRecord( 'postType', 'attachment', 27, { _embed: 'post' } );
  1. Try the above but with the id of an image that is not attached to a post. You shouldn't see post in the list of _links.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@andrewserong
andrewserong marked this pull request as ready for review September 24, 2025 02:47
@github-actions

github-actions Bot commented Sep 24, 2025 •

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. 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 andrewserong, ramonopoly, mukesh27, adamsilverstein.

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

@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

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

@andrewserong

Copy link
Copy Markdown
Contributor Author

@rmccue and @spacedmonkey just adding you as reviewers for this one as I know you've both done a lot of REST API work in the past. I've had a bit of a break from contributing directly to core this year, so apologies for the ping if you're not the right folks to ping on these sorts of PRs (and of course let me know if there's anyone else I should request a review from). Thanks!

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking good to me. Thanks for the tests. I had a question about the post type some folks might be able to verify.

if ( ! empty( $post->post_parent ) ) {
$links['post'] = array(
'href' => rest_url( rest_get_route_for_post( $post->post_parent ) ),
'embeddable' => true,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at other controllers, embeddable links often include additional attributes. Example:

https://github.com/ramonjd/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L1193Looking at other controllers, embeddable links often include additional attributes

Would it be useful to know the post type before embedding?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question! That could also be useful for requests that aren't embedding, but that would like to fetch the linked resource in a separate request, as they'd know the post type 👍

I'm curious for feedback on this, as we'd need to add a call to get_post to grab the post type, but overall I like the idea!

@ramonjd ramonjd Sep 30, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would like to fetch the linked resource in a separate request, as they'd know the post type

A use case could be interacting with the wordpress/core-data package, or directly linking to an editor instance, e.g., /wp-admin/site-editor.php?p=/page/3&canvas=edit.

Access to the post type and id is required.

Having said that, it's something that can be added later so unless there's a perceived and medium-term need for it, I think it's okay to leave out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Really good points, I'm happy to update it, I think it'll make the link far more useful to include the post type 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 770e64e to add in the post_type and ids:

image

How's that looking?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice

Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php Outdated
@andrewserong
andrewserong force-pushed the add/post-to-attachment-links-in-rest-api-response branch from cb08d80 to 770e64e Compare September 30, 2025 05:02

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Image

This is a small change and (in my opinion) non-controversial.

It makes sense to be able to embed an attachment's attached post.

There is talk about allowing multiple parents for attachments, e.g.,

They are 12+ years old though, so I think any changes to many-to-many relationships for these post types can be dealt with if it ever gets in.

@andrewserong

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I'll just ping another couple of folks for visibility in case anyone else has opinions on the shape of this change. FYI: @swissspidy and @adamsilverstein in case this touches upon any media stuff y'all are working on.

@adamsilverstein adamsilverstein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changes look fine to me, thanks @andrewserong!

@andrewserong

Copy link
Copy Markdown
Contributor Author

Thanks everyone! I'll look to commit this today

@github-actions

github-actions Bot commented Oct 3, 2025

Copy link
Copy Markdown

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

SVN changeset: 60893
GitHub commit: 2da7a59

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

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.

4 participants