Error while trying to include blocks in wp-api

Issue Summary

I’ve just installed Local for the first time. I’m on a Windows 11 machine and the Wordpress 6.0 site is running on nginx, php 7.4.1, and MySQL 8.0.16. I’m building a headless site with React on the client and I’m attempting to get block data for posts in my api request using the code blatantly stolen from here… https://wpscholar.com/blog/add-gutenberg-blocks-to-wp-rest-api/

It looks like…

add_action(
	'rest_api_init',
	function () {

		if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
			require ABSPATH . 'wp-admin/includes/post.php';
		}

		// Surface all Gutenberg blocks in the WordPress REST API
		$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,
					'blocks',
					[
						'get_callback' => function ( array $post ) {
							return parse_blocks( $post['content']['raw'] );
						},
					]
				);
			}
		}
	}
);

Every time I include this code in functions. php any blocks in a post vanish after navigating away the post editor. The content is still there and can be seen in a preview, but the blocks are gone from the list view and cannot be seen in the post.

Troubleshooting Questions

I’m not sure if this is going to happen on the live server or not. That will likely be my next step.
I’ve tried the same thing in Laragon with the same results. Have not tried XAMP yet.
I have searched the Wordpress docs to make sure nothing is deprecated. I have also searched for a newer method of revealing parsed blocks to the api, without luck.

Replication

Describe the steps that others can take to replicate this issue. If you have screenshots that can help clarify what is happening, please include them!

System Details

  • Which version of Local is being used?
    6.4.0+5927

  • What Operating System (OS) and OS version is being used?
    Windows 11

  • Attach the Local Log. See this Help Doc for instructions on how to do so:

Security Reminder

Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there’s always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn’t private info like passwords being displayed.

2 Likes

Hey, I’m experiencing the same thing. Did you ever find a solution or work-around?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.