Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Created April 6, 2019 08:26
Show Gist options
  • Save obiPlabon/c7cd2c05dda1d1758846209346185628 to your computer and use it in GitHub Desktop.
Save obiPlabon/c7cd2c05dda1d1758846209346185628 to your computer and use it in GitHub Desktop.
<?php
add_action(
'rest_api_init',
function () {
if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
require ABSPATH . 'wp-admin/includes/post.php';
}
// Surface all Gutenberg blocks in the WordPress REST API
$post_types = get_post_types_by_support( [ 'editor' ] );
foreach ( $post_types as $post_type ) {
if ( use_block_editor_for_post_type( $post_type ) ) {
register_rest_field(
$post_type,
'blocks',
[
'get_callback' => function ( array $post ) {
return parse_blocks( $post['content']['raw'] );
},
]
);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment