Skip to content

Instantly share code, notes, and snippets.

@lukasbesch
Last active October 12, 2022 18:12
Show Gist options
  • Save lukasbesch/ed9cdd5c7df0eb620b2b9cb48fd7935c to your computer and use it in GitHub Desktop.
Save lukasbesch/ed9cdd5c7df0eb620b2b9cb48fd7935c to your computer and use it in GitHub Desktop.
WP REST Blocks – Empty editor fix
<?php
/**
* Plugin Name: WP REST Blocks – Empty editor fix
* Plugin URI: https://gist.github.com/lukasbesch/ed9cdd5c7df0eb620b2b9cb48fd7935c
* Description: Fixes the empty editor issue with the »WP REST Blocks« plugin on WordPress 5.9+. See https://github.com/spacedmonkey/wp-rest-blocks/issues/31
* Version: 0.1.0
*
* @see https://github.com/spacedmonkey/wp-rest-blocks/issues/31
*/
namespace WP_REST_Blocks\Hotfixes\EmptyEditorInWP5_9;
add_action( 'rest_api_init', __NAMESPACE__ . '\\wp_rest_blocks_init', 11 );
function wp_rest_blocks_init() {
if ( ! function_exists('\\WP_REST_Blocks\\Posts\\get_post_types_with_editor') ) {
return;
}
$types = \WP_REST_Blocks\Posts\get_post_types_with_editor();
if ( empty( $types ) ) {
return;
}
// This is only necessary if we are in the block editor at the moment.
if (! function_exists('\\get_current_screen')) {
return;
}
$screen = \get_current_screen();
if ( ! ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) ) {
return;
}
global $wp_rest_additional_fields;
foreach ( $types as $object_type ) {
if ( ! isset( $wp_rest_additional_fields[ $object_type ][ 'blocks' ] ) ) {
continue;
}
// Store the original args for later re-registration
$args = $wp_rest_additional_fields[ $object_type ][ 'blocks' ];
// "De-register" the field "blocks" using the global (unfortunately there is no unregister_rest_field function)
unset( $wp_rest_additional_fields[ $object_type ][ 'blocks' ] );
// Re-register the field under another key "parsed_blocks"
\register_rest_field(
$object_type,
'parsed_blocks',
$args
);
}
}
@FaisalAli19
Copy link

Hi,

Can you help me with the location of the file where I need to add this code to fix the issue?

Thanks in advance.

@lukasbesch
Copy link
Author

You can just drop this file into the plugins directory and activate it.

@lukasbesch
Copy link
Author

You can just drop this file into the plugins directory and activate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment