Created
September 1, 2018 09:18
-
-
Save mcsf/1f67c86a5e40dbbf630def4625348b7c to your computer and use it in GitHub Desktop.
(For testing purposes) WP API endpoint for parsing a post for blocks.
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 | |
/** | |
* Plugin Name: Blocks endpoint for Posts | |
* Description: — | |
*/ | |
add_action( 'rest_api_init', 'mcsf_register_post_blocks_endpoint' ); | |
function mcsf_register_post_blocks_endpoint() { | |
register_rest_route( | |
'wp/v2', '/posts/(?P<id>[\d]+)/blocks', array( | |
'args' => array( | |
'id' => array( | |
'description' => 'Unique identifier', | |
'type' => 'integer', | |
), | |
), | |
array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'mcsf_get_post_blocks', | |
), | |
) | |
); | |
} | |
function mcsf_get_post_blocks( $request ) { | |
$post = get_post( $request['id'] ); | |
if ( is_wp_error( $post ) ) { | |
return $post; | |
} | |
$data = gutenberg_parse_blocks( $post->post_content ); | |
$response = rest_ensure_response( $data ); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment