WP_Interactivity_API::data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array $context_stack, array $namespace_stack )

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.

Processes the data-wp-context directive.


It adds the context defined in the directive value to the stack so that it’s available for the nested interactivity elements.


The directives processor instance.
Whether the processing is entering or exiting the tag.
The reference to the context stack.
The reference to the store namespace stack.


private function data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
	// When exiting tags, it removes the last context from the stack.
	if ( 'exit' === $mode ) {
		array_pop( $context_stack );

	$attribute_value = $p->get_attribute( 'data-wp-context' );
	$namespace_value = end( $namespace_stack );

	// Separates the namespace from the context JSON object.
	list( $namespace_value, $decoded_json ) = is_string( $attribute_value ) && ! empty( $attribute_value )
		? $this->extract_directive_value( $attribute_value, $namespace_value )
		: array( $namespace_value, null );

	 * If there is a namespace, it adds a new context to the stack merging the
	 * previous context with the new one.
	if ( is_string( $namespace_value ) ) {
		$context_stack[] = array_replace_recursive(
			end( $context_stack ) !== false ? end( $context_stack ) : array(),
			array( $namespace_value => is_array( $decoded_json ) ? $decoded_json : array() )
	} else {
		 * If there is no namespace, it pushes the current context to the stack.
		 * It needs to do so because the function pops out the current context
		 * from the stack whenever it finds a `data-wp-context`'s closing tag.
		$context_stack[] = end( $context_stack );



User Contributed Notes

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