InnerBlocks doesn't work anymore on 1.26 on Rest API

Support MB Blocks InnerBlocks doesn't work anymore on 1.26 on Rest API

  • This topic has 5 replies, 2 voices, and was last updated 1 year ago by PeterPeter.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #45332
    JulienJulien
    Participant

    Hi,

    I update from 1.25.x to 1.26.2 and inside Rest API of wordpress, <InnerBlocks /> is not interprated.

    It's ok on the editor, it's ok on standard webpage but on Rest API it's not.

    Screenshot from Gutenberg
    From Gutenberg

    Screenshot from website
    From Website

    Screenshot from Rest API
    From API

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $meta_boxes[] = [
            'title'           => 'Hero Content',
            'id'              => 'hero-content',
            'description'     => 'A custom hero content block',
    
            // Block settings.
            'type'            => 'block',
            'icon'            => 'awards',
            'category'        => 'layout',
            'context'         => 'side',
            'render_callback' => function($attributes){
                echo '<header>'.$attributes['title'].'</header><section><InnerBlocks /></section>';
            },
            'enqueue_style'   => get_template_directory_uri() . '/blocks/hero/style.css',
            'supports' => [
                'align' => ['wide', 'full'],
            ],
    
            // Block fields.
            'fields'          => [
                [
                    'id'   => 'title',
                    'name' => 'Title',
                ],
            ],
        ];
        return $meta_boxes;
    } );

    General informations
    This test come from a new wordpress from scratch.
    Edit first article, push the code inside functions.php of twentytwentyfour

    Wordpress Version : 6.5.2
    Meta Box: 5.9.7
    MetaB Box AIO: 1.26.2 (only MB Block activated)
    Theme Twenty Twenty-Four 1.1
    PHP 8.2.18 (came from docker image)

    Activated plugins
    Meta Box
    Meta Box AIO

    If I switch to meta box aio 1.25 all is fine

    #45333
    JulienJulien
    Participant

    Sorry for the images

    I can't edit

    Screenshot from Gutenberg

    Screenshot from website

    Screenshot from Rest API

    #45334
    JulienJulien
    Participant

    Ok I found the problem

    We use WordPress only for backend and we use NextJS for the front-end so we need to have API return correct html.

    Can you adapt with a referer or something else please ?

    #45335
    JulienJulien
    Participant

    Quick fix could be use with REST_REQUEST: $_GET['context'] === 'edit'

                   // If block is called via WP-API, thats mean we are in the editor, we need to keep <InnerBlocks /> tag.
    		if ( defined( 'REST_REQUEST' ) && REST_REQUEST && isset($_GET['context']) && $_GET['context'] === 'edit' ) {
    			return $rendered;
    		}
    
    		$rendered = str_replace( '$', '\$', $rendered );
    		$rendered = preg_replace( '#<InnerBlocks([^>]*?)/>#', $content, $rendered );
    
    		return $rendered;
    #45389
    JulienJulien
    Participant

    Any news ?

    #45394
    PeterPeter
    Moderator

    Hello Julien,

    Thank you for your feedback.

    I've shared your idea with the development team to support other JS libraries to get the rendered HTML via Rest API. I will let you know when I have a response from the dev team.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.