Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created September 1, 2018 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsf/1f67c86a5e40dbbf630def4625348b7c to your computer and use it in GitHub Desktop.
Save mcsf/1f67c86a5e40dbbf630def4625348b7c to your computer and use it in GitHub Desktop.
(For testing purposes) WP API endpoint for parsing a post for blocks.
<?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