Created
December 9, 2019 15:19
-
-
Save them-es/f5d227aca7cb724ae3ddb8668c8a9415 to your computer and use it in GitHub Desktop.
Include Gutenberg blocks in WP-API response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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