Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yeah, sounds like an obvious move. Looks like the reusable block appears as a comment in the post content, and that doesn’t get picked up by Relevanssi. I’ll have to find some documentation and find out how those blocks can be expanded, but yes, this is something you can expect to find in the next version of Relevanssi.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, looks like that wasn’t terribly difficult. Try adding this to your theme functions.php:

    add_filter( 'relevanssi_post_content', 'relevanssi_gutenberg_block_rendering', 10, 2 );
    function relevanssi_gutenberg_block_rendering( $content, $post ) {
    	if ( gutenberg_content_has_blocks( $post->post_content ) ) {
    		$blocks = gutenberg_parse_blocks( $post->post_content );
    	}
    	foreach ( $blocks as $block ) {
    		$attributes = (array) $block->attrs;
    		$render = render_block_core_block( $attributes );
    		$content .= $render;
    	}
    	return $content;
    }

    That should handle the reusable blocks.

    Thread Starter slimmilkduds

    (@slimmilkduds)

    Wow, thanks for the extremely speedy support! That worked like a charm

    Thread Starter slimmilkduds

    (@slimmilkduds)

    This doesn’t seem to work anymore, I assume Gutenberg has changed something in the last few versions since you wrote that snippet.

    Thread Starter slimmilkduds

    (@slimmilkduds)

    I checked with the Gutenberg devs and by a quick glance they suggested changing gutenberg_content_has_blocks to has_blocks or removing "if ( gutenberg_content_has_blocks( $post->post_content ) ) since gutenberg_parse_blocks includes a check for has_blocks. What I am left with is this:

    add_filter( 'relevanssi_post_content', 'relevanssi_gutenberg_block_rendering', 10, 2 );
    function relevanssi_gutenberg_block_rendering( $content, $post ) {
            $blocks = gutenberg_parse_blocks( $post->post_content );
    
            foreach ( $blocks as $block ) {
                    $attributes = (array) $block->attrs;
                    $render = render_block_core_block( $attributes );
                    $content .= $render;
            }
            return $content;
    }
    

    This still doesn’t work however, the index is build but content in reusable blocks isn’t expanded.

    Plugin Author Mikko Saari

    (@msaari)

    This seems to work:

    function relevanssi_gutenberg_block_rendering( $content, $post ) {
    	$blocks = gutenberg_parse_blocks( $post->post_content );
    
    	foreach ( $blocks as $block ) {
    		$attributes = (array) $block['attrs'];
    		$render     = render_block_core_block( $attributes );
    		$content   .= $render;
    	}
    	return $content;
    }

    That is: $block is now an array, not an object.

    Thread Starter slimmilkduds

    (@slimmilkduds)

    Yay! It’s working again, thanks a lot!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Gutenberg reusable block search’ is closed to new replies.