Skip to content

Instantly share code, notes, and snippets.

@them-es
Created December 9, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save them-es/f5d227aca7cb724ae3ddb8668c8a9415 to your computer and use it in GitHub Desktop.
Save them-es/f5d227aca7cb724ae3ddb8668c8a9415 to your computer and use it in GitHub Desktop.
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