You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm trying to make a headless website with the Gutenberg editor.
For now, I'm facing a problem with passing block data as JSON response through REST API endpoint, because, as I mentioned, on save method, not all attributes are saved to block markup.
I'll try to describe the steps that I've made to achieve my plan:
Extended base REST API for receiving page data:
/** * Include all Gutenberg blocks from content in REST API response * * */functiongutenberg_blocks_to_rest_api($response, $post, $request)
{
if (!function_exists('parse_blocks')) {
return$response;
}
if (isset($post)) {
$response->data['blocks'] = parse_blocks($post->post_content); // https://developer.wordpress.org/reference/functions/parse_blocks
}
return$response;
}
add_filter('rest_prepare_post', 'gutenberg_blocks_to_rest_api', 10, 3);
add_filter('rest_prepare_page', 'gutenberg_blocks_to_rest_api', 10, 3);
Created my custom Gutenberg block with these attributes:
On the screenshot below, I've snipped results of request:
As you can see, just one attribute of four loaded (and if it's value will be the same as default, zero attributes will load).
Effect that I'm trying to achieve here - using almost the same code on the edit/save function and on the frontend with full react support bypassing all attributes to request.
Any idea how I can achieve this?
The text was updated successfully, but these errors were encountered:
Unfortunately, currently there's no server-side (PHP) capability for applying these defaults. That's tracked in #2751, so you wouldn't be able to apply the defaults in your gutenberg_blocks_to_rest_api function, unless you did this using your own implementation.
I'm not sure what your best option would be here, the crux seems to be that your application has a different flow to the post editor, which stores block data as HTML and parses that data client side, while your application parses server side and is then exposed to the problems outlined in #2751.
Hi, I'm trying to make a headless website with the Gutenberg editor.
For now, I'm facing a problem with passing block data as JSON response through REST API endpoint, because, as I mentioned, on save method, not all attributes are saved to block markup.
I'll try to describe the steps that I've made to achieve my plan:
and following save method:
On the screenshot below, I've snipped results of request:
As you can see, just one attribute of four loaded (and if it's value will be the same as default, zero attributes will load).
Effect that I'm trying to achieve here - using almost the same code on the edit/save function and on the frontend with full react support bypassing all attributes to request.
Any idea how I can achieve this?
The text was updated successfully, but these errors were encountered: