Skip to content

Instantly share code, notes, and snippets.

@pnettto
Created July 29, 2020 14:32
Show Gist options
  • Save pnettto/15aa9c7a855eccf78c717a2491372074 to your computer and use it in GitHub Desktop.
Save pnettto/15aa9c7a855eccf78c717a2491372074 to your computer and use it in GitHub Desktop.
Surface Guntenberg Block in WP Rest API
<?php
// Surface all Gutenberg blocks in the WordPress REST API
function add_blocks_to_rest_api() {
$post_types = get_post_types_by_support([ 'editor' ]);
foreach ($post_types as $post_type) {
register_rest_field($post_type, 'blocks', [
'get_callback' => function (array $post) {
return parse_blocks($post['content']['raw']);
}
]);
}
}
add_action( 'rest_api_init', 'add_blocks_to_rest_api' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment