Open tomhrtly opened 5 years ago
@tomhrtly https://wpscholar.com/blog/add-gutenberg-blocks-to-wp-rest-api/
Had the same question today, but solved it with this advice.
Thanks @gustavpursche! Will leave this open incase @airesvsg has any input
Hi @tomhrtly,
Thanks for you suggestion.
Maybe I'll release this feature, I want to waiting more for it.
At this time I can write some add-on to extend it. First of all, I'll study to understand how to work.
Thanks again
Thanks @airesvsg, looking forward to it :)
@gustavpursche - correct me if I'm wrong, but the link you're referring to is using the out of the box WP REST API and not the ACF to REST API, right?
I believe this feature request would be to have the ACF to REST API plugin support serving ACF Blocks as well. Admittedly I could see how this could be considered a "nice to have", I think it would be awesome if there are those of us that already have apps using ACF to REST could continue down that path with Gutenberg ACF blocks, without having to either wholesale convert to the WP default API, write our own code to splice in that information, or some other mix of things to get there. So +1 in the corner of ACF to REST supporting this! Thanks for what yal do :)
@alexbarbato Yes, I'm referring to a out of the box solution. As far as I understand ACF blocks, the data is directly written into the body of the post (as JSON) and not anymore into the meta table and therefore the plugin doesn't make that much sense anymore for these cases.
If you have a post type without editor support it's still needed. Anyway: I agree - the plugin could provide a layer of consistency across different versions and cases though :)
I tried to get blocks working last night to no avail. I'm now using lazyblocks and a customised version of https://github.com/royboy789/gutenberg-object-plugin instead
I'd much prefer to use ACF and ACF to Rest though, @airesvsg any update on blocks support?
I'm also interested. My end goal is to use lots of ACF-only blocks within gutenberg for my headless wordpress instance, rather than using ACF Flexible Content. Has anyone got this working, or is Flexible Content the only way?
FYI, here is some code i've been using on projects:
Here's a plugin I made that does the job
function wp_acf_rest_api_blocks_init() {
if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
require ABSPATH . 'wp-admin/includes/post.php';
}
$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,
'gblocks',
['get_callback' => function ( array $post ) {
$blocks = apply_filters( 'rest_response_parse_blocks', json_decode( json_encode( parse_blocks( $post['content']['raw'] ) ) ), $post, $post_type );
return array_values(array_filter($blocks, fn($block) => $block->blockName));
}]
);
}
}
}
add_action('rest_api_init', 'wp_acf_rest_api_blocks_init');
if( function_exists('acf_register_block_type') ) {
function wp_acf_rest_api_blocks_filter_response( $blocks ) {
foreach($blocks as &$block) {
if(strpos($block->blockName, 'acf/') === 0) {
acf_setup_meta( json_decode(json_encode($block->attrs->data), true), $block->attrs->id, true );
$id = $block->attrs->id;
unset($block->attrs->name);
unset($block->attrs->id);
unset($block->attrs->data);
$block->attrs = (object) array_merge((array) $block->attrs, get_fields($id));
}
if(!empty($block->innerBlocks) && is_array($block->innerBlocks)) {
$block->innerBlocks = wp_acf_rest_api_blocks_filter_response($block->innerBlocks);
}
}
return $blocks;
}
add_filter('rest_response_parse_blocks', 'wp_acf_rest_api_blocks_filter_response');
}
Also - @terence1990 - I tried out your plugin - as I assumed it would add a rest api endpoint for the blocks (called gblocks).
The usual ACF end points would be: /wp-json/acf/v3/posts/164 ( 164 is a post id example here)
Where does this function add/create the end point(s)? - I can't seem to find them, unless I'm fundamentally misunderstanding what your function does.
Even though ACF Blocks is still in beta, it would be great if this plugin could support the ACF Blocks feature so that we can read the data saved inside fields within a block created with ACF.