Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active March 15, 2022 01:19
Show Gist options
  • Save wpscholar/65ab29b11caad8e0efb93ca01609a979 to your computer and use it in GitHub Desktop.
Save wpscholar/65ab29b11caad8e0efb93ca01609a979 to your computer and use it in GitHub Desktop.
Surface all Gutenberg blocks in the WordPress REST API
<?php
add_action('rest_api_init', function() {
// Surface all Gutenberg blocks in the WordPress REST API
$post_types = get_post_types_by_support( [ 'editor' ] );
foreach ( $post_types as $post_type ) {
if ( gutenberg_can_edit_post_type( $post_type ) ) {
register_rest_field( $post_type, 'blocks', [
'get_callback' => function ( array $post ) {
return gutenberg_parse_blocks( $post['content']['raw'] );
}
] );
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment