Surface all Gutenberg blocks in the WordPress REST API
This file contains 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 | |
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