ClassicPress PetitionsClassicPress Petitions
This is a read-only archive. Post or comment on the live version of this page on our forums.

Add featured +- attached images to post endpoint in REST API

November 27, 2018 · 17:27 · Laurence Bahiirwa
Description

When one pulls up the endpoint "wp-json/wp/v2/posts" we get a key called "featured_media" but it returns an integer value.

Can't we rewrite this to give a proper URL to the featured image? or even add another key that has attached images as URLs.

This will prevent the extra queries one has to return the image URLs or even install bloated plugins to get this information.

I know the endpoint already has so much information one rarely needs thus the growth of tech like GraphQL but we could also consider Author name & description to the individual posts.

This is voice is for decoupling ClassicPress to modern apps or front-ends.

Voters
Tags
Difficulty: Hard
Request: Add feature
Discussion
Laurence Bahiirwa

I am doing something for testing here. https://github.com/bahiirwa/Extend-REST-api/

James Nylen

I don't think this is necessary. You can use /wp-json/wp/v2/posts?_embed instead.

This returns more information than desired, in your case. Here is a proposed change that will help with this issue in a backwards-compatible way: https://core.trac.wordpress.org/ticket/39696

James Nylen

In the meantime, this is fairly easy to do in a plugin. Here is some code to use as a starting point: https://github.com/Automattic/jetpack/pull/9968/files#diff-a9fa774ba2c32ad36ba4e9fc601d8eb0

Laurence Bahiirwa

wp-json/wp/v2/posts?_embed is not always reliable from my experience. Errors are returned thats why I always edit the endpoint with the code shared. It also adds alot of lines of code on endpoint making it slower.