Title: A first look at the Interactivity API
Author: Magdalena Paciorek
Published: April 11, 2024

---

 * [Showcase](https://wordpress.org/showcase/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Themes](https://wordpress.org/themes/)
 * [Hosting](https://wordpress.org/hosting/)
 * [News](https://wordpress.org/news/)
    - [Learn WordPress](https://learn.wordpress.org/)
    - [Documentation](https://wordpress.org/documentation/)
    - [Education](https://wordpress.org/education/)
    - [Forums](https://wordpress.org/support/forums/)
    - [Developers](https://developer.wordpress.org/)
    - [Blocks](https://wordpress.org/blocks/)
    - [Patterns](https://wordpress.org/patterns/)
    - [Photos](https://wordpress.org/photos/)
    - [Openverse ︎](https://openverse.org/)
    - [WordPress.tv ︎](https://wordpress.tv/)
 *  - [About WordPress](https://wordpress.org/about/)
    - [Make WordPress](https://make.wordpress.org/)
    - [Events](https://events.wordpress.org/)
    - [Five for the Future](https://wordpress.org/five-for-the-future/)
    - [Enterprise](https://wordpress.org/enterprise/)
    - [Gutenberg ︎](https://wordpress.org/gutenberg/)
    - [Job Board ︎](https://jobs.wordpress.net/)
 * [Swag ︎](https://mercantile.wordpress.org/)
 * [Get WordPress](https://wordpress.org/download/)

Search in WordPress.org

 [ Get WordPress ](https://wordpress.org/download/)

[WordPress Developer Blog](https://developer.wordpress.org/news)

A first look at the Interactivity API

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

# A first look at the Interactivity API

By [Magdalena Paciorek](https://profiles.wordpress.org/magdalenapaciorek/)

April 11, 2024

[Advanced](https://developer.wordpress.org/news/category/advanced/), [Blocks](https://developer.wordpress.org/news/category/blocks/)

The Interactivity API was included in WordPress 6.4 as a private API that could 
only be used by Core blocks. With the release of WordPress 6.5, the API has been
made public and is now available for developers to use in their custom projects.

Table of Contents

 1. [General overview of the Interactivity API](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#general-overview-of-the-interactivity-api)
 2. [Prerequisites](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#prerequisites)
 3. [Creating a basic toggle block](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#creating-a-basic-toggle-block)
 4.  a. [wp-interactive directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-interactive-directive)
     b. [wp-context directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-context-directive)
     c. [wp-on directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-on-directive)
     d. [wp-bind directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-bind-directive)
     e. [wp-class directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-class-directive)
     f. [wp-style directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-style-directiveanother-approach-to-hiding-the-paragraph-would-be-to-add-an-inline-style-to-the-paragraph-with-the-value-of-display-none-the-interactivity-api-includes-a-wp-style-directive-that-adds-or-removes-inline-styles-from-the-element)
 5. [Creating a donation calculator](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#creating-a-donation-calculator)
 6.  a. [Adding price control in the editor](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#adding-price-control-in-the-editor)
     b. [Adding styles to the block](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#adding-styles-to-the-block)
     c. [Creating the frontend view of the block](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#creating-the-frontend-view-of-the-block)
     d.  a. [wp-text directive](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#wp-text-directive)
 7. [Resources to learn more about the Interactivity API](https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/#resources-to-learn-more-about-the-interactivity-api)

Until recently, Gutenberg development primarily focused on enhancing the backend
of blocks, which mainly affected content creators. The Interactivity API, on the
other hand, focuses on the frontend.

Typically, users interact with the website by clicking or scrolling through the 
page. The Interactivity API allows you to add behaviors to blocks that will happen
in response to user interactions. These may include simple effects, such as showing
or hiding elements on click or animating elements on scroll, as well as more advanced
features like navigation without page reloads or instant search.

A practical example of a feature built with the Interactivity API is the lightbox
option in the Core Image block, added in WordPress 6.4.

Traditionally, features like this one have a long history of being coded with jQuery,
and even today, many themes and plugins continue to rely on it. The Interactivity
API aims to decrease jQuery usage in WordPress projects and become the new standard
for coding interactive elements.

## General overview of the Interactivity API

The Interactivity API shares many similarities with other JS frameworks, especially
Alpine.js, which makes it accessible for anyone already familiar with the modern
JavaScript ecosystem.

It extends standard HTML with directives, which are special data attributes that
can listen to and modify the behavior of any DOM element. With directives, you can
manipulate the DOM, apply CSS styles, handle user input, and much more.

It’s important to highlight that the block creation workflow doesn’t change. To 
add interactivity to a block you need to extend it with new features of the Interactivity
API. You still can use the [@wordpress/create-block](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/)
scaffolding tool and develop the editor part of the block just as you normally do.

To add interactivity to blocks using the Interactivity API, you need to:

 * **Add directives** as data attributes to the HTML markup to add specific behaviors
   to the block. You would usually add them in the `render.php` file within the 
   block directory (only dynamic blocks are supported in WordPress 6.5). The list
   of all publicly available directives can be found in the [Interactivity API Reference](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives).
 * **Create a store** with the logic (state, actions, or callbacks) needed for interactivity.
   Typically, it is added in the `view.js` file within the block directory. In the
   store, **actions** can be defined as functions that run in response to user interaction,
   such as a click. Actions update the global **state** or the local** context**,
   which, in turn, updates the HTML element connected to either of them. Besides
   actions, you can also define **callbacks** that would run as side effects. Callbacks
   are also functions, but are not directly invoked by user interactions, they are
   invoked by the state change. You can read more about the store in the [documentation](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#the-store).

The following section of this article will demonstrate how to code a block for a
charity that allows users to calculate how many trees will be planted for the donation
they plan to make. By the end, you should have a good grasp of how to use the Interactivity
API.

This is what you’re going to build:

## Prerequisites

This tutorial expects that you are familiar with the fundamentals of building custom
blocks, such as development environment, file structure, `block.json` metadata, 
static vs. dynamic rendering of a block, etc. It also assumes that you’ve already
built at least a basic block. If this is not the case, I would recommend reading
the [Build your first block](https://developer.wordpress.org/block-editor/getting-started/tutorial/)
tutorial first.

## **Creating a basic toggle block**

The first step is to scaffold the initial block structure using the Interactivity
API template [@wordpress/create-block-interactive-template](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block-interactive-template/).
It’s best to run it from the `/plugins` directory in a local WordPress installation.

    ```line-numbers
    npx @wordpress/create-block@latest donation-calculator --template @wordpress/create-block-interactive-template
    ```

Next, use the command `cd donation-calculator` to navigate to the block directory
and start the development with `npm start`.

The scaffolding tool has created a basic interactive toggle block, which should 
be available for use in the editor after the plugin is activated in the WordPress
admin panel.

Let’s now inspect the generated code to understand what makes this block interactive.

Open the `block.json` file in the`/src` folder. In the `supports` section, you should
see that the `interactivity` is set to `true`. This line is necessary to enable 
the Interactivity API in the block. Also, ensure that the `viewScriptModule` property
is defined, and the path to the `view.js` file is correct.

    ```language-json
    "supports": {
    	"interactivity": true
    },
    "viewScriptModule": "file:./view.js"
    ```

### **wp-interactive directive**

Now open the `render.php` file with the frontend markup of the block.

    ```php
    <?php
    $unique_id = wp_unique_id( 'p-' );
    ?>

    <div
    	<?php echo get_block_wrapper_attributes(); ?>
    	data-wp-interactive="create-block"
    	<?php echo wp_interactivity_data_wp_context( array( 'isOpen' => false ) ); ?>
    	data-wp-watch="callbacks.logIsOpen"
    >
    	<button
    		data-wp-on--click="actions.toggle"
    		data-wp-bind--aria-expanded="context.isOpen"
    		aria-controls="<?php echo esc_attr( $unique_id ); ?>"
    	>
    		<?php esc_html_e( 'Toggle', 'donation-calculator' ); ?>
    	</button>

    	<p
    		id="<?php echo esc_attr( $unique_id ); ?>"
    		data-wp-bind--hidden="!context.isOpen"
    	>
    		<?php
    			esc_html_e( 'Donation Calculator - hello from an interactive block!', 'donation-calculator' );
    		?>
    	</p>
    </div>
    ```

A few data attributes have been added to the HTML markup. These are the directives
that were mentioned earlier in the article. They all follow `data-wp-directiveName`
syntax.

The [wp-interactive directive](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp-interactive)
activates interactivity for the wrapping `<div>` and its children through the Interactivity
API. It includes a namespace `create-block` to reference the store defined in the`
view.js` file.

    ```javascript
    import { store, getContext } from '@wordpress/interactivity';

    store( 'create-block', {
    	actions: {
    		toggle: () => {
    			const context = getContext();
    			context.isOpen = ! context.isOpen;
    		},
    	},
    	callbacks: {
    		logIsOpen: () => {
    			const { isOpen } = getContext();
    			// Log the value of `isOpen` each time it changes.
    			console.log( `Is open: ${ isOpen }` );
    		},
    	},
    } );
    ```

The namespace in the `wp-interactive` directive should match the first argument 
of the store function. You can change the `create-block` namespace to the one that
matches the plugin name – the `donation-calculator` in this example. This change
needs to be updated in both files – `render.php` and `view.js`.

### wp-context directive

The interaction in this block is quite common – it shows or hides the paragraph 
when the user clicks on the button. To store information about the paragraph’s visibility,
you need a context. With the Interactivity API, you can use [wp_interactivity_data_wp_context()](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp_interactivity_data_wp_context)
function for this, which can be added to the wrapping `<div>`. The context is local
and is available to `<div>` and all its children, but not to other blocks.

The `wp_interactivity_data_wp_context()` function returns a stringified JSON of 
a context directive. In this block, `wp-context` stores information about the visibility
of the paragraph under the `isOpen` key, with the initial value of `false`. This
value will later be used to conditionally add or remove the `hidden` attribute from
the paragraph in response to user interaction.

    ```php
    <div
    	<?php echo get_block_wrapper_attributes(); ?>
    	data-wp-interactive="donation-calculator"
    	<?php echo wp_interactivity_data_wp_context( array( 'isOpen' => false ) ); ?>
    	data-wp-watch="callbacks.logIsOpen"
    >
    ```

### **wp-on directive**

Next, an event listener needs to be added to the button to handle the click. The
Interactivity API provides a [wp-on directive](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp-on)
to listen to DOM events.

The directive follows the syntax `data-wp-on--[event]`.

In this example, the `data-wp-on--click` directive is added to the button. The name
of the function, `toggle`, declared in the store as an action, is passed as a value.

    ```php
    <button
    	data-wp-on--click="actions.toggle"
    	data-wp-bind--aria-expanded="context.isOpen"
    	aria-controls="<?php echo esc_attr( $unique_id ); ?>"
    >
    	<?php esc_html_e( 'Toggle', 'donation-calculator' ); ?>
    </button>
    ```

Next, the logic needs to be created – what should happen when the user clicks the
button. Now, because the Interactivity API follows a declarative approach, the event
triggered by user interaction doesn’t directly modify the HTML element. Instead,
it should update the state or the context, and the Interactivity API will take care
of updating the element connected to either of them.

In this block, when the user clicks on the button, it triggers an action that sets
the `isOpen` context to the opposite value (eg., from `false` to `true`). The Interactivity
API then automagically shows or hides the `<p>` element.

Let’s open the `view.js` file and inspect the `toggle()` function defined in the`
actions` property of the store.

    ```javascript
    store( 'donation-calculator', {
    	actions: {
    		toggle: () => {
    			const context = getContext();
    			context.isOpen = ! context.isOpen;
    		},
    	},
    });
    ```

The `toggle()` function first retrieves the current context properties with `getContext()`
and then sets the `isOpen` property to the opposite value. That’s all the function
does.

To verify if the logic works as expected, you can add `console.log( context.isOpen);`
under the last line of the `toggle()` function. Now, when you click on the button,
you should see in the console that the value changes with every click.

The Interactivity API also provides directives for window and document event listeners.
For further details, you can refer to the [documentation](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp-on-window).

### wp-bind directive

To make the toggle block work, you need to connect the paragraph that you want to
show or hide to the `isOpen` context. You use the [wp-bind directive](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp-bind),
which allows setting HTML attributes on elements based on a boolean or string value.

The directive follows the syntax `data-wp-bind--[attribute]`.

In this example, a `hidden` attribute should be added to the paragraph only when
the `isOpen` context is set to `false`. So the `data-wp-bind--hidden` attribute 
needs to be added to the `<p>` element with the opposite value of `context.isOpen`
assigned to it.

    ```php
    <p
    	id="<?php echo esc_attr( $unique_id ); ?>"
    	data-wp-bind--hidden="!context.isOpen"
    >
    	<?php
    	esc_html_e( 'Donation Calculator - hello from an interactive block!', 'donation-calculator' );
    	?>
    </p>
    ```

When `isOpen` is set to `false`, the `hidden` attribute will be added to `<p>`, 
so it will no longer be visible. When it is set to `true`, the `hidden` attribute
will be removed and the paragraph will be visible again.

### wp-class directive

Alternatively, you could hide the paragraph by adding a class `.hidden`, which would
have a CSS property `display` set to `none`. The Interactivity API includes a [wp-class directive](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp-class)
that adds or removes a class conditionally based on a boolean value.

The directive follows the syntax `data-wp-class--[classname]`.

Let’s go ahead and replace the `data-wp-bind--hidden` attribute on the paragraph
with `data-wp-class--hidden`, and leave the value as it is. You also need to add
the `.hidden` `{display: none}` code to the `style.scss` file in the block directory.

    ```php
    <p
    	id="<?php echo esc_attr( $unique_id ); ?>"
    	data-wp-class--hidden="!context.isOpen"
    >
    	<?php
    	esc_html_e( 'Donation Calculator - hello from an interactive block!', 'donation-calculator' );
    	?>
    </p>
    ```

When `isOpen` is set to false, the `class="hidden"` attribute will be added to `
<p>`, making it no longer visible. When it is set to true, the class will be removed.

### wp-style directive

Another approach to hiding the paragraph would be to add an inline style to the 
paragraph, with the value of `display:none`. The Interactivity API includes a wp-
style directive that adds or removes inline styles from the element.

The directive follows the syntax `data-wp-style--[css-property]`.

To use the `display` CSS property, you need to add the `data-wp-style--display` 
directive to the `<p>` element and set it to either `none` or `block`. You can’t
use the `context.isOpen` directly as value because it returns `true` or `false`,
but you can use it as a base for a **derived state** that will be computed from 
it.

The Interactivity API store has another property called **state**, where you can
define the derived state or context using a getter function. Let’s create a `display()`
getter. It will return none if `context.isOpen` is `false` and `block` when `true`.

    ```javascript
    store( 'donation-calculator', {
    	state: {
    		get display() {
    			const context = getContext();
        		      return context.isOpen ? 'block' : 'none';
    		}
    	},
    	actions: {
    		toggle: () => {
    			const context = getContext();
    			context.isOpen = ! context.isOpen;
    		}
    	},
    });
    ```

You can then use the `display()` getter in the `data-wp-style--display` directive,
treating it like any other state property, referring to it as `state.display`. Let’s
add it to the paragraph.

    ```php
    <p
    	id="<?php echo esc_attr( $unique_id ); ?>"
    	data-wp-style--display="state.display"
    >
    	<?php
    	esc_html_e( 'Donation Calculator - hello from an interactive block!', 'donation-calculator' );
    	?>
    </p>
    ```

## Creating a donation calculator

Let’s modify the block to create a donation calculator for a charity organization.
It will have a form where site visitors can enter the amount they are willing to
donate. The calculator will then calculate and display the number of trees the organization
can plant for the specified donation. The block will allow the website admin to 
set the price of planting one tree, which will be used in the calculations.

There are a few steps that you can take:

 * define an attribute that will store the price (`block.json`)
 * add a control where the editor can set the price for planting one tree and include
   a visual preview of a form (`edit.js`)
 * make a few changes to the HTML markup of the block, such as adding a form, adding
   the Interactivity API directives etc. (`render.php`)
 * define an action and getter functions that will handle calculations (`view.js`)
 * add the style for the block (`style.scss` and `editor.scss`)

### Adding price control in the editor

First, define a `price` attribute to store the price of planting a tree set by the
person editing the page. Add the `attributes` property to the existing elements 
of the `block.json`. The `price` attribute will have a type of `number` and a default
value which will be used by the calculator in case the editor won’t set the price.

    ```language-json
    "attributes": {
        "price": {
          "type": "number",
          "default": 15
        }
     },
    ```

Next, create the editor view for the block. Add this code to the `edit.js` file.

    ```javascript
    import { __ } from '@wordpress/i18n';
    import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
    import { __experimentalNumberControl as NumberControl, PanelBody
    } from '@wordpress/components';

    export default function Edit( { attributes, setAttributes } ) {

    	const { price } = attributes;
    	const exampleDonationAmount = 1000;
            const trees = Math.floor( exampleDonationAmount / price );

    	return (
    		<div { ...useBlockProps() }>
    			<InspectorControls>
    				<PanelBody title={ __( 'Calculator settings' ) }>
    					<NumberControl
    						label={ __( 'Set the price for one tree' ) }
    						help={ __( 'The value will be used for calculations.' ) }
    						value={ price }
    						min={ 1 }
    						onChange={ 
    							( value ) => 
    								setAttributes( { 
    									price: Number( value ) 
    								} ) 
    						}
    					/>
    				</PanelBody>
    			</InspectorControls>
    			<form className="calculator">
    				<label for="contribution-value" className="calculator-label">{ __( 'Check the impact of your donation:' ) }</label>
    				<div class="calculator-input">$
    					<input disabled value={ exampleDonationAmount } className="calculator-input-form" type="number" id="contribution-value"/>
    				</div>
    				<output className="calculator-output">
    				    {[
    				    	__( 'Your ' ), 
    				    	<span>${ exampleDonationAmount }</span>, 
    				    	__( ' donation will enable us to plant ' ),
    				    	<span>{ trees }</span>,
    				    	__( ' trees.' )
    				    ]}
    				</output>
    			</form>
    		</div>
    	);
    }
    ```

You won’t be able to add the Interactivity API to the backend of the block in WordPress
6.5. It will serve as a preview of the form with some predefined data and a control
for the price. The calculator will only work on the front end for site visitors.

### Adding styles to the block

You also need to add some styles to the block. Go ahead and copy the following CSS
and add it to the `style.scss` file in the block directory.

    ```language-css
    .wp-block-create-block-donation-calculator {
    	box-sizing: border-box;
    	background-color: #f2fcf6;
    	color: #023a51;
    	border: 3px solid #023a51;
    	border-radius: 1.5rem;
    	text-align: center;
    	overflow: hidden;

    	.calculator {
    		padding: 3rem 2rem;

    		&-input {
    			margin: 1.25rem auto;
    			font-size: 1.75rem;
    			color: #023a51;
    			display: flex;
    			justify-content: center;
    			align-items: center;

    			&-form{
    				padding: 0.5rem 1rem;
    				margin-left: 0.5rem;
    				border: 2px solid #023a51;
    				border-radius: 1rem;
    				background-color: #fff;
    				font-size: 1.5rem;
    				color: #023a51;
    				max-width: 130px;
    			}
    		}
    		&-output {
    			display: none;

    			&.show {
    				display: block;
    			}

    			span {
    				color: #f2fcf6;
    				background: #0cab49;
    				font-weight: bold;
    				border-radius: 5px;
    				padding: 0.25rem 0.5rem;
    			}
    		}
    	}
    }
    ```

Add the style for the backend (in the `editor.scss`) to make the info paragraph 
visible at all times in the editor.

    ```language-css
    .wp-block-create-block-donation-calculator  {
    	.calculator-output {
    		display: block;
    	}
    }
    ```

The editor view of the block:

![editor view of the donation calculator block](https://developer.wordpress.org/
news/files/2024/04/interactivity-api-donation-calculator-admin.jpg)

### Creating the frontend view of the block

The price control in the editor allows the user to set the price of planting a tree.
The value is stored in the `price` attribute, which can be accessed in the `render.
php` file through the `$attributes` array, under its key, as defined in `block.json`.

You can access the price via `$attributes['price']`, which stores the user-entered
value or the default of `15`.

The price is passed to the context so that the Interactivity API can use it. You
can use the [wp_interactivity_data_wp_context()](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#wp_interactivity_data_wp_context)
function to set the initial context for the `price` and for the `contribution`, 
which will store the value typed by the user in the input field.

Remove everything from the `render.php` file and add new markup for the calculator
form.

    ```php
    <?php
    $context = array(
      	'price' => (int)$attributes['price'],
      	'contribution' => 0
    );
    ?>

    <div 
    	<?php echo get_block_wrapper_attributes(); ?>
    	data-wp-interactive="donation-calculator"
    	<?php echo wp_interactivity_data_wp_context( $context ); ?>
    >
    	<form aria-label="<?php esc_attr_e( 'Calculate the impact of your donation.' ); ?>" class="calculator">
    		<label for="contribution-value" class="calculator-label"><?php esc_html_e( 'Check the impact of your donation:' ); ?></label>
            <div class="calculator-input">$
    		  	<input 
    				data-wp-on--input="actions.calculate" 
    				placeholder="0" 
    				type="number" 
    				id="contribution-value" 
    				class="calculator-input-form"
    			>
    		</div>	
    	</form>
    </div>
    ```

The frontend of the block should now look like this:

![front end of the donation calculator block](https://developer.wordpress.org/news/
files/2024/04/interactivity-api-donation-calculator-frontend.jpg)

Notice the `data-wp-on--input="actions.calculate"` directive added to the input 
element.

Once the user starts typing, the `calculate()` function defined under the actions
property of the `store` will be invoked. The number typed into the form is passed
to the function as an event object (e) and accessed as `e.target.value` property.
The `calculate()` function will save the value in the `context.contribution` property.

The short info box should show up under the form, stating how many trees can be 
planted for the amount the user is willing to donate. These two values can be derived
from the `context.contribution`. The first one (number of trees) is calculated by
dividing the donation value by the price and the second one (the value with a $ 
sign) is created by converting the number to a string.

Lastly, the form output is only displayed once the user enters a number greater 
than 0 in the form. The value of the style is derived from the `context.contribution`.

Under the state property, let’s add the following getter functions:

 * `donation()`: returns the formatted value with the $ sign as a string
 * `trees()` : returns the result of dividing the value by the price, rounded down
 * `show()` : returns `true` if the user-entered value is greater than 0, or `false`
   otherwise

This logic needs to be added in the `view.js` file.

    ```javascript
    import { store, getContext } from '@wordpress/interactivity';

    store( 'donation-calculator', {
    	state: {
    		get donation() {
    			const context = getContext();
    			return `$${context.contribution}`;
    		},
    		get trees() {
    			const context = getContext();
    			return Math.floor( context.contribution / context.price );
    		},
    		get show() {
    			const context = getContext();
        		return context.contribution > 0;
    		}
    	},
    	actions: {
    		calculate: ( e ) => {
    			const context = getContext();
    			context.contribution = Number( e.target.value );
    		}
    	}
    } );
    ```

#### wp-text directive

The only task remaining is to display information about the contribution below the
form.

The Interactivity API includes a `wp-text` directive used to set the inner HTML.
You need to add it to the `<span>` tags inside the `<output>` element.

Then you connect the `wp-text` directive to the corresponding state getters, linking
the first `<span>` in the `<output>` to the `state.donation`. This displays the 
user-entered value in the form with a $ sign. The second `<span`> is connected to`
state.trees` displaying the calculated number of trees that can be planted for the
donation.

The `wp-text` directive manages the setting of the inner HTML for these spans, re-
rendering it every time a user types a number into the form field.

To complete this exercise, you will add the `data-wp-class--show="state.show"` directive
to the `output` element. You’ll find the whole code for `render.php` below.

    ```php
    <?php
    $context = array(
      	'price' => (int)$attributes['price'],
      	'contribution' => 0
    );
    ?>

    <div 
    	data-wp-interactive="donation-calculator"
    	<?php echo wp_interactivity_data_wp_context( $context ); ?>
    	<?php echo get_block_wrapper_attributes(); ?>
    >
    	<form aria-label="<?php esc_attr_e( 'Calculate the impact of your donation.' ); ?>" class="calculator">
    		<label for="contribution-value" class="calculator-label"><?php esc_html_e( 'Check the impact of your donation:' ); ?></label>
            <div class="calculator-input">$
    		  	<input 
    				data-wp-on--input="actions.calculate" 
    				placeholder="0" 
    				type="number" 
    				id="contribution-value" 
    				class="calculator-input-form"
    			>
            </div>	
            <output 
            	class="calculator-output" 
            	data-wp-class--show="state.show"
            >
    			<?php
    			echo sprintf(
    			    esc_html__( 'Your %s donation will enable us to plant %s trees.' ),
    			    '<span data-wp-text="state.donation"></span>',
    				'<span data-wp-text="state.trees"></span>'
    			);?>
    		</output>
    	</form>
    </div>
    ```

The below video shows the completed block on the frontend.

## Resources to learn more about the Interactivity API

 * [API Reference](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/)–
   the official documentation
 * [WP Briefing discussing the Interactivity API](https://wordpress.org/news/2024/02/episode-73-inside-the-interactivity-api)
 * Dev Note: [Interactivity API in 6.5](https://make.wordpress.org/core/2024/03/04/interactivity-api-dev-note/)
 * [Developer Hours: Exploring the Interactivity API in WordPress 6.5](https://www.youtube.com/watch?v=Sl6nqGN8YVQ)
 * [Developer Hours: Building custom blocks with the Interactivity API](https://www.youtube.com/watch?v=ndW-Kz0iDdY)

The Interactivity API is currently under development, with additional features still
on the roadmap. For further details, you can refer to this [GitHub issue](https://github.com/WordPress/gutenberg/issues/60219).

_Props to [@bph](https://profiles.wordpress.org/bph/), [@luisherranz](https://profiles.wordpress.org/luisherranz/),
[@cbravobernal](https://profiles.wordpress.org/cbravobernal/), [@jonsurrell](https://profiles.wordpress.org/jonsurrell/),
[@welcher](https://profiles.wordpress.org/welcher/), [@greenshady](https://profiles.wordpress.org/greenshady/)
for providing feedback and reviewing this post._

**Share the post: **

**Categories:** [Advanced](https://developer.wordpress.org/news/category/advanced/),
[Blocks](https://developer.wordpress.org/news/category/blocks/)

**Tags:** [Block development](https://developer.wordpress.org/news/tag/block-development/),
[Interactivity API](https://developer.wordpress.org/news/tag/interactivity-api/)

## 18 responses to “A first look at the Interactivity API”

 1.  ![ts Avatar](https://secure.gravatar.com/avatar/ae52bb959a4e2ad9a909a2cb5f7bcbe872640536c0ad2244f175e23180e5c921?
     s=40&d=identicon&r=g)
 2.  ts
 3.  [April 12, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-4920)
 4.  “You won’t be able to add the Interactivity API to the backend of the block in
     WordPress 6.5.”
 5.  Thanks for the tutorial! One question: so basically, if I understand correctly,
     at this point, in case the backend/edit representation of the interactive block
     is supposed to mirror the frontend functionality, it will have to be (re-)written
     in react (so: backend: react, frontend: interactivity API). Your sentence above
     implies that this may change in future releases. Is it planned that the interactivity
     API can somehow be used in the editor?.
 6.  Thanks!
 7.  [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=4920#respond)
 8.   a. ![Magdalena Paciorek Avatar](https://secure.gravatar.com/avatar/43dacba6ead4e05bdfa29c0a3b0247ed3f2f031c1572eae667387eb56b6e7089?
         s=40&d=identicon&r=g)
      b. [Magdalena Paciorek](https://profiles.wordpress.org/magdalenapaciorek/)
      c. [April 13, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5034)
      d. That’s correct, currently, you’ll need to recreate the interaction in React for
         the editor.
      e. It was mentioned in the Interactivity API Proposal article ([https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/](https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/))
         that the team would like to explore the possibility of reusing some directives
         across both the frontend and the editor to unify the entire block developer experience.
         Although it is not on the roadmap for WordPress 6.6, it’s something the team 
         has in mind.
      f. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5034#respond)
      g.  a. ![justin Avatar](https://secure.gravatar.com/avatar/927bf077a6b2e25b4dcea0ac105501ebae59c93289f35cf7ac76faec8bdfd566?
             s=40&d=identicon&r=g)
          b. justin
          c. [April 14, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5148)
          d. loved the article one question will this eventually be possible to use for custom
             made plugins or will this be specifically for wp editor
          e. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5148#respond)
 9.  ![Ralf Zschemisch Avatar](https://secure.gravatar.com/avatar/f31ed6b8d9006f588a41d96c460a244a3b900493ac716a201a472c88fcda4b3a?
     s=40&d=identicon&r=g)
 10. Ralf Zschemisch
 11. [April 14, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5156)
 12. Thank you for the comprehensive introduction to the topic. Personally, I had hoped
     that using the Interactivity API would be a little easier.
 13. [code]
      (base) PS C:xampphtdocswordpresswp-contentdonation-calculator&gt; npm 
     start
 14. &gt; donation-calculator@0.1.0 start
      &gt; wp-scripts start –experimental-modules
 15. assets by status 15.5 KiB [emitted]
      asset index.js 13.4 KiB [emitted] (name: 
     index) 1 related asset asset ./style-index.css 1.02 KiB [emitted] (name: ./style-
     index) (id hint: style) 1 related asset asset index.css 1 KiB [emitted] (name:
     index) 1 related asset asset index.asset.php 134 bytes [emitted] [compared for
     emit] (name: index) asset render.php 1.12 KiB [compared for emit] [from: src/render.
     php] [copied] asset block.json 590 bytes [compared for emit] [from: src/block.
     json] [copied] Entrypoint index 15.5 KiB (11 KiB) = ./style-index.css 1.02 KiB
     index.css 1 KiB index.js 13.4 KiB index.asset.php 134 bytes 3 auxiliary assets
     runtime modules 5.19 KiB 14 modules orphan modules 5.03 KiB [orphan] 4 modules
     built modules 3.03 KiB (javascript) 569 bytes (css/mini-extract) [built] javascript
     modules 2.52 KiB cacheable modules 2.36 KiB modules by path ./src/*.js 2.26 KiB
     2 modules modules by path ./src/*.scss 100 bytes 2 modules external [&quot;wp&
     quot;,&quot;blocks&quot;] 42 bytes [built] [code generated] external &quot;React&
     quot; 42 bytes [built] [code generated] external [&quot;wp&quot;,&quot;i18n&quot;]
     42 bytes [built] [code generated] external [&quot;wp&quot;,&quot;blockEditor&quot;]
     42 bytes [built] [code generated] css modules 569 bytes css ./node_modules/css-
     loader/dist/cjs.js??ruleSet[1].rules[4].use[1]!./node_modules/postcss-loader/dist/
     cjs.js??ruleSet[1].rules[4].use[2]!./node_modules/sass-loader/dist/cjs.js??ruleSet[
     1].rules[4].use[3]!./src/style.scss 295 bytes [built] [code generated] css ./node_modules/
     css-loader/dist/cjs.js??ruleSet[1].rules[4].use[1]!./node_modules/postcss-loader/
     dist/cjs.js??ruleSet[1].rules[4].use[2]!./node_modules/sass-loader/dist/cjs.js??
     ruleSet[1].rules[4].use[3]!./src/editor.scss 274 bytes [built] [code generated]./
     src/block.json 526 bytes [built] [code generated] webpack 5.91.0 compiled successfully
     in 1099 ms
 16. assets by status 3.02 KiB [cached] 1 asset
      asset view.asset.php 130 bytes [emitted](
     name: view) Entrypoint view 3.15 KiB (2.6 KiB) = view.js 3.02 KiB view.asset.php
     130 bytes 1 auxiliary asset cached modules 484 bytes (javascript) 274 bytes (runtime)[
     cached] 3 modules webpack 5.91.0 compiled successfully in 246 ms
 17. [/code]
 18. Sorry that’s too complicated for maintenance
 19. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5156#respond)
 20.  a. ![Justin Tadlock Avatar](https://secure.gravatar.com/avatar/8013a62d7397c006bae48b96d6832a573ea71a7d5d3597fe584210567b508c05?
         s=40&d=identicon&r=g)
      b. [Justin Tadlock](https://profiles.wordpress.org/greenshady/)
      c. [April 15, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5176)
      d. I don’t understand how the webpack compilation message you copied is related 
         to maintenance. It looks like it was successful. Can you expand on what the issue
         is?
      e. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5176#respond)
 21. ![sinanisler Avatar](https://secure.gravatar.com/avatar/b89af680647c05d2cec17ff0371b876f2eb8015c892d2c2a59153694d56f4cc5?
     s=40&d=identicon&r=g)
 22. [sinanisler](https://profiles.wordpress.org/sinanisler/)
 23. [April 15, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5166)
 24. Amazing tutorial thank you very much. ❤ love the great details.
 25. lately, I have been using and implementing lots of htmx on my wp projects. I think
     instead of using interactivity API I will use htmx .org instead.
 26. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5166#respond)
 27.  a. ![justin Avatar](https://secure.gravatar.com/avatar/927bf077a6b2e25b4dcea0ac105501ebae59c93289f35cf7ac76faec8bdfd566?
         s=40&d=identicon&r=g)
      b. justin
      c. [April 15, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5172)
      d. very curious on how you use htmx in your projects. personally i haven’t dabbled
         yet with it but it looks interesting.
      e. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5172#respond)
 28. ![Asif Iqbal Avatar](https://secure.gravatar.com/avatar/8a073465a80e0c8045a57fc82ac313d21a392e5b4658286e6c44f4667813bd37?
     s=40&d=identicon&r=g)
 29. Asif Iqbal
 30. [April 16, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5182)
 31. It is a first glimpse for me that WordPress evolving, I will definitely try this
     out.
 32. Thank you for the great article.
 33. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5182#respond)
 34. ![Silas Köhler Avatar](https://secure.gravatar.com/avatar/1eb1c62dc9aae57a181c0bf6d99f5ad767648360e0ab7c9609d0fdab66ffc972?
     s=40&d=identicon&r=g)
 35. [Silas Köhler](http://silaskoehler.de)
 36. [April 16, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5184)
 37. Thanks for this great article!
 38. honestly i am not 100% sure when i should use a callback and when i should use
     an action. You write: “actions […] run in response to user interaction, such as
     a click. […] callbacks […] as side effects. Callbacks are also functions, but 
     are not directly invoked by user interactions, they are invoked by the state change.
     You can read more about the store in the documentation. ”
 39. Unfortunately, I couldn’t find an explanation for the distinction between actions
     and callbacks in the documentation.
 40. The image block e.g. uses a callback on “window–resize” and an action on “window–
     scroll”. Isn’t window resize also a user interaction?
      ([https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/image/index.php#L265](https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/image/index.php#L265))
 41. Is there a technical difference between actions and callbacks or is it more of
     a logical difference?
 42. I hope my questions are understandable.
 43. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5184#respond)
 44. ![Hammad Mustafa Avatar](https://secure.gravatar.com/avatar/ef2eed03d34c2658de12824a0738fb6f2a2714ed3292ae86237bcb082e7a39db?
     s=40&d=identicon&r=g)
 45. Hammad Mustafa
 46. [April 20, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5439)
 47. Thanks you very much to inform about pieces of interactive events of API with 
     practically demonstration.
 48. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5439#respond)
 49. ![Alen Avatar](https://secure.gravatar.com/avatar/c77241070a14d162a2635d33b5ec4b243dd1399b53e4b810d95fdbacb13873f3?
     s=40&d=identicon&r=g)
 50. [Alen](https://micemade.com)
 51. [April 22, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5559)
 52. I’m using “viewScript” in block.json file, and all my fronted stuff is happening
     there (I’m fetching data via REST API in separate JS file, I don’t use render.
     php).
      What is relation (is there any) between “viewScript” and “viewScriptModule”?
     Is “render.php” the only way to use Interactivity API directives, or directives
     can be used in frontend JS code?
 53. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5559#respond)
 54. ![HelgaTheViking Avatar](https://secure.gravatar.com/avatar/d4015f6437cf56ccff9d0beb2085f38f94e240969384f8301a540d3eee6d33d4?
     s=40&d=identicon&r=g)
 55. [HelgaTheViking](https://profiles.wordpress.org/helgatheviking/)
 56. [April 24, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5680)
 57. I found out the hard way that you seemingly cannot use a custom webpack.config.
     js file and use the `-experimental-modules` flag. Is that going to change as hopefully
     the interactivity compiling comes out from behind the experimental flag?
 58. Question, does the Interactivity API only work on elements that are rendered in
     the original DOM? I was able to make the example script work in a block as part
     of a single post, but then when i (admittedly very hackily – or shall we say exploratorily)
     rendered it via `do_blocks()` in the WooCommerce cart block (which is entirely
     rendered on the front end) the interactivity stopped working (as in clicking on
     the toggle button didn’t reveal the additional text).
 59. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5680#respond)
 60.  a.  ![silaskoehler Avatar](https://secure.gravatar.com/avatar/1eb1c62dc9aae57a181c0bf6d99f5ad767648360e0ab7c9609d0fdab66ffc972?
          s=40&d=identicon&r=g)
      b.  [silaskoehler](https://profiles.wordpress.org/silaskoehler/)
      c.  [April 25, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5685)
      d.  Hello Helga,
      e.  Regarding your first question: I think it is possible to use a custom webpack.
          config.js file and the `-experimental-modules` flag. But you have to change 
          the way you handle the defaultConfig. At least that was the point where I had
          problems.
      f.  Without the flag the defaultConfig gives you an Object with the config back,
          with the flag the defaultConfig gives you a Array of two Objects back.
           [https://github.com/WordPress/gutenberg/blob/3a38dd82e9abff09747e1db0ae989b9d1cc67c84/packages/scripts/config/webpack.config.js#L469](https://github.com/WordPress/gutenberg/blob/3a38dd82e9abff09747e1db0ae989b9d1cc67c84/packages/scripts/config/webpack.config.js#L469)
      g.  i use ‘webpack-merge’ to override both.
      h.  [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5685#respond)
      i.   a. ![HelgaTheViking Avatar](https://secure.gravatar.com/avatar/d4015f6437cf56ccff9d0beb2085f38f94e240969384f8301a540d3eee6d33d4?
              s=40&d=identicon&r=g)
           b. [HelgaTheViking](http://kathyisawesome.com)
           c. [May 13, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-5998)
           d. Hi @silaskoehler – would you happen to have an example I could look at? This
              seems like an important, but hidden, nugget of information. 🙂
           e. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=5998#respond)
           f.  a. ![Silas Köhler Avatar](https://secure.gravatar.com/avatar/1eb1c62dc9aae57a181c0bf6d99f5ad767648360e0ab7c9609d0fdab66ffc972?
                  s=40&d=identicon&r=g)
               b. [Silas Köhler](http://silaskoehler.de)
               c. [May 14, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-6021)
               d. Hi @HelgaTheViking,
               e. i try to write an example here: [https://gist.github.com/Taigistal/d2baa8320e3222f0d24ba301eb2ecfdb](https://gist.github.com/Taigistal/d2baa8320e3222f0d24ba301eb2ecfdb)
               f. Sorry for my English, I hope it’s understandable
               g. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=6021#respond)
 61. ![gresch Avatar](https://secure.gravatar.com/avatar/911ccdbbd8dfa7de54043fcf3a70f04b662b55010ac149e4236f724ffda669b0?
     s=40&d=identicon&r=g)
 62. [gresch](https://profiles.wordpress.org/gresch/)
 63. [May 24, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-6293)
 64. Hi Magdalena, thank you very much for this article! IT helped me a lot understanding
     and using the Interactivity API 🙂
 65. IMHO, it’d be just great if it could be extended to a full CRUD course, so I submitted
     [https://github.com/WordPress/Learn/issues/2461](https://github.com/WordPress/Learn/issues/2461).
 66. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=6293#respond)
 67. ![DaveyJake Avatar](https://secure.gravatar.com/avatar/840aee95ab53538583403a65511d3740b11058ec2b8b3b1c24e5f18047c5c297?
     s=40&d=identicon&r=g)
 68. [DaveyJake](https://profiles.wordpress.org/daveyjake/)
 69. [May 24, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-6305)
 70. I sense a lot of inspiration coming from Vue.js?
 71. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=6305#respond)
 72. ![Bipster Avatar](https://secure.gravatar.com/avatar/d289bfdf199c9db04bf2692ef5b358fdae4beb9e6b1ed4c093c52a8e8fbb08d6?
     s=40&d=identicon&r=g)
 73. Bipster
 74. [October 7, 2024](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/#comment-12554)
 75. I’m creating an interactive block that needs to display HTML content from a post
     retrieved using the REST API. However, I noticed that the wp-text directive shows
     raw HTML instead of rendering it as formatted HTML. Is there another directive
     that can properly render HTML content?
 76. Thanks in advance
 77. [Reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?replytocom=12554#respond)

### Leave a Reply 󠀁[Cancel reply](https://developer.wordpress.org/news/2024/04/a-first-look-at-the-interactivity-api/?output_format=md#respond)󠁿

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

 Save my name, email, and website in this browser for the next time I comment.

 Notify me of follow-up comments by email.

 Notify me of new posts by email.

Δ

## Have an idea for a new post?

### 󠀁[Learn how to contribute](https://developer.wordpress.org/news/how-to-contribute/)󠁿

Share your knowledge with fellow WordPress developers.

Search

### 󠀁[Review tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)󠁿

Everything you need to know about writing for the Blog.

## Subscribe to the Blog

  Email Address

 Join 1,946 other subscribers

 * [About](https://wordpress.org/about/)
 * [News](https://wordpress.org/news/)
 * [Hosting](https://wordpress.org/hosting/)
 * [Privacy](https://wordpress.org/about/privacy/)

 * [Showcase](https://wordpress.org/showcase/)
 * [Themes](https://wordpress.org/themes/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Patterns](https://wordpress.org/patterns/)

 * [Learn](https://learn.wordpress.org/)
 * [Documentation](https://wordpress.org/documentation/)
 * [Developers](https://developer.wordpress.org/)
 * [WordPress.tv ](https://wordpress.tv/)

 * [Get Involved](https://make.wordpress.org/)
 * [Events](https://events.wordpress.org/)
 * [Donate ](https://wordpressfoundation.org/donate/)
 * [Swag ](https://mercantile.wordpress.org/)

 * [WordPress.com ](https://wordpress.com/?ref=wporg-footer)
 * [Matt ](https://ma.tt/)
 * [bbPress ](https://bbpress.org/)
 * [BuddyPress ](https://buddypress.org/)

 The WordPress® trademark is the intellectual property of the WordPress Foundation.

 * [Visit our X (formerly Twitter) account](https://www.x.com/WordPress)
 * [Visit our Bluesky account](https://bsky.app/profile/wordpress.org)
 * [Visit our Mastodon account](https://mastodon.world/@WordPress)
 * [Visit our Threads account](https://www.threads.net/@wordpress)
 * [Visit our Facebook page](https://www.facebook.com/WordPress/)
 * [Visit our Instagram account](https://www.instagram.com/wordpress/)
 * [Visit our LinkedIn account](https://www.linkedin.com/company/wordpress)
 * [Visit our TikTok account](https://www.tiktok.com/@wordpress)
 * [Visit our YouTube channel](https://www.youtube.com/wordpress)
 * [Visit our Tumblr account](https://wordpress.tumblr.com/)