Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active March 15, 2022 01:19
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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