Created
July 29, 2020 14:32
-
-
Save pnettto/15aa9c7a855eccf78c717a2491372074 to your computer and use it in GitHub Desktop.
Surface Guntenberg Block in WP Rest API
This file contains hidden or 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 | |
// 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