make_before_block_visitor( array $hooked_blocks, WP_Block_Template|WP_Post|array $context, callable $callback = 'insert_hooked_blocks' ): callable

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Returns a function that injects the theme attribute into, and hooked blocks before, a given block.

Description

The returned function can be used as $pre_callback argument to traverse_and_serialize_block(s), where it will inject the theme attribute into all Template Part blocks, and prepend the markup for any blocks hooked before the given block and as its parent’s first_child, respectively.

This function is meant for internal use only.

Parameters

$hooked_blocksarrayrequired
An array of blocks hooked to another given block.
$contextWP_Block_Template|WP_Post|arrayrequired
A block template, template part, wp_navigation post object, or pattern that the blocks belong to.
$callbackcallableoptional
A function that will be called for each block to generate the markup for a given list of blocks that are hooked to it.
Default: 'insert_hooked_blocks'.

Default:'insert_hooked_blocks'

Return

callable A function that returns the serialized markup for the given block, including the markup for any hooked blocks before it.

Source


/*
 * Skip meta generation if post type is not set.
 */
if ( ! isset( $post->post_type ) ) {
	return $post;
}

$attributes = array();

$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $ignored_hooked_blocks ) ) {
	$ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
	$attributes['metadata'] = array(
		'ignoredHookedBlocks' => $ignored_hooked_blocks,
	);
}

if ( 'wp_navigation' === $post->post_type ) {
	$wrapper_block_type = 'core/navigation';
} elseif ( 'wp_block' === $post->post_type ) {
	$wrapper_block_type = 'core/block';
} else {
	$wrapper_block_type = 'core/post-content';
}

$markup = get_comment_delimited_block_content(
	$wrapper_block_type,
	$attributes,
	$post->post_content
);

$existing_post = get_post( $post->ID );
// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.

Changelog

VersionDescription
6.5.0Added $callback argument.
6.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.