wp_interactivity_data_wp_context()WP 6.5.0

Generates a data-wp-context directive attribute by encoding a context array.

This helper function simplifies the creation of data-wp-context directives by providing a way to pass an array of data, which encodes into a JSON string safe for direct use as a HTML attribute value.

Example:

<div <?php echo wp_interactivity_data_wp_context( array( 'isOpen' => true, 'count' => 0 ) ); ?>>

Хуков нет.

Возвращает

Строку. A complete data-wp-context directive with a JSON encoded value representing the context array and the store namespace if specified.

Использование

wp_interactivity_data_wp_context( $context, $store_namespace ): string;
$context(массив) (обязательный)
The array of context data to encode.
$store_namespace(строка)
The unique store namespace identifier.
По умолчанию: ''

Список изменений

С версии 6.5.0 Введена.

Код wp_interactivity_data_wp_context() WP 6.7.1

function wp_interactivity_data_wp_context( array $context, string $store_namespace = '' ): string {
	return 'data-wp-context=\'' .
		( $store_namespace ? $store_namespace . '::' : '' ) .
		( empty( $context ) ? '{}' : wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) ) .
		'\'';
}