Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Include Gutenberg blocks in WP-API response
<?php
/**
* Include all Gutenberg blocks from content in REST API response
*
*
*/
function my_theme_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', 'my_theme_blocks_to_rest_api', 10, 3 );
add_filter( 'rest_prepare_page', 'my_theme_blocks_to_rest_api', 10, 3 );
//add_filter( 'rest_prepare_{my_custom_posttype}', 'my_theme_blocks_to_rest_api', 10, 3 ); // Custom posttype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.