WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 2 months ago

#53603 new enhancement

Expose block data directly through REST API endpoints

Reported by: gziolo Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 5.8
Component: REST API Keywords: needs-patch dev-feedback
Focuses: Cc:

Description

Part of "PHP APIs Overview" in Gutenberg: https://github.com/WordPress/gutenberg/issues/8352.

Originally reported in https://github.com/WordPress/gutenberg/issues/4763 by @adamsilverstein.

Consider adding Gutenberg block data to the post endpoints - so when retrieving a post via the REST API you could get the block data as part of the content object.

This provides a way to get the data/content of each block of a Gutenberg-built page from the front end. This would be very useful for building component based front ends, because the components could map one-to-one with gutenberg blocks. With these endpoints, an App could easily get the data it needs to render each component. This might also provide a patch for a standard component library matching Gutenberg blocks.

POC PR for this: https://github.com/WordPress/gutenberg/pull/2649

Related ticket with changes proposed to get_posts function: #53602.

Change History (5)

#1 @gziolo
3 months ago

There is also a plugin from @spacedmonkey that adds probably exactly the same functionality:

https://github.com/spacedmonkey/wp-rest-blocks

#2 @spacedmonkey
3 months ago

The biggest issue with extracting data from blocks, is attributes that are css selectors for html tags contents or attributes. Image blocks are the prefect example, as src and alt attributes are stored in the html.

In my plugin, I used a library called pQuery, as a way to use css selectors in PHP. Whatever goes into core, may want to use a different approach.

#3 @gziolo
3 months ago

In my plugin, I used a library called pQuery, as a way to use css selectors in PHP. Whatever goes into core, may want to use a different approach.

So you went one step ahead and mirrored how the blocks are parsed from the inner HTML in JavaScript. That's cool.

The other question is whether we do a good job dynamically injecting attributes in PHP that are derived from the supports field in Block API - things like align, colors, class names, etc.

#4 @adamsilverstein
3 months ago

  • Keywords needs-patch dev-feedback added
  • Milestone changed from Awaiting Review to Future Release

#5 @gziolo
2 months ago

There is also a prototype build 2 years ago for server-side block attributes sourcing:
https://github.com/WordPress/gutenberg/pull/18414

Note: See TracTickets for help on using tickets.