# apply_block_hooks_to_content()

URL: https://chugunov.pro/api-wordpress/functions/apply_block_hooks_to_content/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 6.6.0.

## Сигнатура

```php
apply_block_hooks_to_content( string $content, WP_Block_Template|WP_Post|array|null $context = null, callable $callback = 'insert_hooked_blocks' ): string
```

## Описание

Запускает алгоритм прикреплённых блоков для переданного содержимого.

## Параметры

- `$content` `string` — обязательный. Сериализованное содержимое.
- `$context` `WP_Block_Template|WP_Post|array|null` — необязательный, по умолчанию `null`. Шаблон блока, часть шаблона, объект записи или паттерн, которому принадлежат блоки. Если задано null, будет вызван get_post(), чтобы использовать текущую запись в качестве контекста.
  
  По умолчанию: null.
- `$callback` `callable` — необязательный, по умолчанию `'insert_hooked_blocks'`. Функция, которая будет вызываться для каждого блока, чтобы сформировать разметку для заданного списка блоков, подключённых к нему.
  
  Значение по умолчанию: 'insert_hooked_blocks'.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/blocks.php:1102`

```php
function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) {
	// Default to the current post if no context is provided.
	if ( null === $context ) {
		$context = get_post();
	}

	$hooked_blocks = get_hooked_blocks();

	$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
	$after_block_visitor  = null;
	if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
		$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
		$after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );
	}

	$block_allows_multiple_instances = array();
	/*
	 * Remove hooked blocks from `$hooked_block_types` if they have `multiple` set to false and
	 * are already present in `$content`.
	 */
	foreach ( $hooked_blocks as $anchor_block_type => $relative_positions ) {
		foreach ( $relative_positions as $relative_position => $hooked_block_types ) {
			foreach ( $hooked_block_types as $index => $hooked_block_type ) {
				$hooked_block_type_definition =
					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );

				$block_allows_multiple_instances[ $hooked_block_type ] =
					block_has_support( $hooked_block_type_definition, 'multiple', true );

				if (
					! $block_allows_multiple_instances[ $hooked_block_type ] &&
					has_block( $hooked_block_type, $content )
				) {
					unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ][ $index ] );
				}
			}
			if ( empty( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
				unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] );
			}
		}
		if ( empty( $hooked_blocks[ $anchor_block_type ] ) ) {
			unset( $hooked_blocks[ $anchor_block_type ] );
		}
	}

	/*
	 * We also need to cover the case where the hooked block is not present in
	 * `$content` at first and we're allowed to insert it once -- but not again.
	 */
	$suppress_single_instance_blocks = static function ( $hooked_block_types ) use ( &$block_allows_multiple_instances, $content ) {
		static $single_instance_blocks_present_in_content = array();
		foreach ( $hooked_block_types as $index => $hooked_block_type ) {
			if ( ! isset( $block_allows_multiple_instances[ $hooked_block_type ] ) ) {
				$hooked_block_type_definition =
					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );

				$block_allows_multiple_instances[ $hooked_block_type ] =
					block_has_support( $hooked_block_type_definition, 'multiple', true );
			}

			if ( $block_allows_multiple_instances[ $hooked_block_type ] ) {
				continue;
			}

			// The block doesn't allow multiple instances, so we need to check if it's already present.
			if (
				in_array( $hooked_block_type, $single_instance_blocks_present_in_content, true ) ||
				has_block( $hooked_block_type, $content )
			) {
				unset( $hooked_block_types[ $index ] );
			} else {
				// We can insert the block once, but need to remember not to insert it again.
				$single_instance_blocks_present_in_content[] = $hooked_block_type;
			}
		}
		return $hooked_block_types;
	};
	add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
	$content = traverse_and_serialize_blocks(
		parse_blocks( $content ),
		$before_block_visitor,
		$after_block_visitor
	);
	remove_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );

	return $content;
}
```

## История изменений

- 6.8.0 — Have the $context parameter default to null, in which case get_post() will be called to use the current post as context.
- 6.7.0 — Injects the theme attribute into Template Part blocks, even if no hooked blocks are registered.
- 6.6.0 — Introduced.

## Связанные

Использует: [`traverse_and_serialize_blocks`](https://chugunov.pro/api-wordpress/functions/traverse_and_serialize_blocks/), [`make_after_block_visitor`](https://chugunov.pro/api-wordpress/functions/make_after_block_visitor/), [`make_before_block_visitor`](https://chugunov.pro/api-wordpress/functions/make_before_block_visitor/), [`get_hooked_blocks`](https://chugunov.pro/api-wordpress/functions/get_hooked_blocks/), [`block_has_support`](https://chugunov.pro/api-wordpress/functions/block_has_support/), `WP_Block_Type_Registry::get_instance`, [`parse_blocks`](https://chugunov.pro/api-wordpress/functions/parse_blocks/), [`has_block`](https://chugunov.pro/api-wordpress/functions/has_block/), [`has_filter`](https://chugunov.pro/api-wordpress/functions/has_filter/), [`add_filter`](https://chugunov.pro/api-wordpress/functions/add_filter/), [`remove_filter`](https://chugunov.pro/api-wordpress/functions/remove_filter/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/).
Используется в: [`apply_block_hooks_to_content_from_post_object`](https://chugunov.pro/api-wordpress/functions/apply_block_hooks_to_content_from_post_object/), [`update_ignored_hooked_blocks_postmeta`](https://chugunov.pro/api-wordpress/functions/update_ignored_hooked_blocks_postmeta/), [`inject_ignored_hooked_blocks_metadata_attributes`](https://chugunov.pro/api-wordpress/functions/inject_ignored_hooked_blocks_metadata_attributes/), [`get_block_file_template`](https://chugunov.pro/api-wordpress/functions/get_block_file_template/), [`_build_block_template_result_from_file`](https://chugunov.pro/api-wordpress/functions/_build_block_template_result_from_file/), [`_build_block_template_result_from_post`](https://chugunov.pro/api-wordpress/functions/_build_block_template_result_from_post/), [`get_block_templates`](https://chugunov.pro/api-wordpress/functions/get_block_templates/), `WP_Block_Patterns_Registry::get_registered`, `WP_Block_Patterns_Registry::get_all_registered`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/apply_block_hooks_to_content/
