<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Food Blogger Mania &#187; Image Block</title>
	<atom:link href="https://foodbloggermania.it/tag/ricette/image-block/feed/" rel="self" type="application/rss+xml" />
	<link>https://foodbloggermania.it</link>
	<description>Food Blogger Mania</description>
	<lastBuildDate>Sun, 05 Apr 2026 10:00:00 +0000</lastBuildDate>
	<language>it-IT</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>santox89</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Ovosodo Cook</dc:creator>
				<category><![CDATA[Emilia Romagna]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Loading React]]></category>
		<category><![CDATA[Performant Optimizing]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Mariapia</dc:creator>
				<category><![CDATA[Basilicata]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Query Loop]]></category>
		<category><![CDATA[Unlike Alpine]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>fucinaidee</dc:creator>
				<category><![CDATA[Veneto]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Mario Santos]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>ricette da coinquiline</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Ragazze conTorte</dc:creator>
				<category><![CDATA[Lazio]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Pani cunzatu</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Alveare Delle Delizie</dc:creator>
				<category><![CDATA[Friuli Venezia Giulia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>BucciaDiArancia</dc:creator>
				<category><![CDATA[Piemonte]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>claudialuca90</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Gemma archeologa tra i fornelli.</dc:creator>
				<category><![CDATA[Toscana]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What’s New on WordPress.com: Focus Mode, Promotion From Your Pocket, and Risk-Free Redesigns</title>
		<link>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-2/</link>
		<comments>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-2/#comments</comments>
		<pubDate>Thu, 20 Apr 2023 20:01:20 +0000</pubDate>
		<dc:creator>claudialuca90</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Select Distraction]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-2/</guid>
		<description><![CDATA[At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&#160; The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits&#160;<a href="https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-2/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&nbsp;</p>
<p>The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits to your work are readily accessible.&nbsp;</p>
<p>Let’s jump in and take a look.&nbsp;&nbsp;&nbsp;</p>
<h2>Write without distractions&nbsp;</h2>
<p>One of writing’s biggest challenges has nothing to do with the words — it’s filtering out the noise (both literal and figurative) so you can focus. We can’t change your physical environment, but we’re doing what we can to help you get into “the zone” a little more easily. Enter distraction free mode, a minimalist WordPress experience that removes the buttons and menus.&nbsp;&nbsp;&nbsp;</p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif"><img src="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif?w=1024" alt="" class="wp-image-51617" width="703" height="483" /></a></figure>
</div>
<div></div>
<p>From the top toolbar, click the three-dot menu on the right-hand side. Select “Distraction free” and you’ll notice the top toolbar disappear. (To bring it back, simply hover over the top of the page and it’ll pop back down.) Your pages and posts will be distraction-free until you deselect this mode. </p>
<p><strong>When to use this feature: </strong>You can’t resist experimenting with various settings or styles, but you also really need to get that new post finished. (Believe us, we’ve been there.) With this cleaner interface, you can focus on your words and your words alone.&nbsp;</p>
<h2>Promote your content from your phone&nbsp;</h2>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/advertising">Blaze</a>, our self-serve ad platform, makes it easier than ever to spread the word about whatever it is you’re writing, creating, or selling. Now, you can Blaze on the go with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2023/02/15/switch-to-the-new-jetpack-mobile-app/">the Jetpack app</a>.&nbsp;</p>
<figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg?w=547" alt="" class="wp-image-51618" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg?w=566" alt="" class="wp-image-51620" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg?w=519" alt="" class="wp-image-51619" /></a></figure>
</figure>
<p>From the main dashboard, simply click on the banner that says “Promote your content with Blaze.” You can also promote your work directly from the Posts or Pages menus by clicking “More” or the three-dot menu and selecting “Promote with Blaze.”&nbsp;</p>
<p><strong>When to use this feature: </strong>You’re waiting for take-out at your favorite Indian restaurant. While scrolling the Jetpack app, you notice traffic spiking on one of your recent blog posts — something about it is resonating with readers. Maximize its impact by boosting it to an even wider audience with Blaze.</p>
<h2>Easily find and add free images&nbsp;</h2>
<p>Need a featured image for your post? You can now find and add free-to-use images even faster. Open the Inserter (the red “+” button at the top left of any post or page), navigate to the tab labeled “Media,” click “Openverse,” and find the perfect option from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://openverse.org/">our favorite open-source library</a>. </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png?w=1024" alt="" class="wp-image-51629" width="581" height="220" /></a></figure>
</div>
<div></div>
<p>You can also still access Openverse from the Image Block or Media Library. </p>
<p><strong>When to use this feature: </strong>You’re on the hunt for just the right image to use in your new blog post. Rather than doing a Google search in a separate tab — and running the risk of using an image you don’t have rights to — use the Inserter to search and place exactly what you need.&nbsp;</p>
<h2>Give readers a timetable</h2>
<p>Here’s a fun built-in feature that a lot of folks don’t know about: The WordPress.com editor’s “Outline” function displays a post’s various headings for easy scanning, as well as a word/character count and a “time to read” estimate of how long it would take an average reader to get through the content on that page: </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png?w=694" alt="" class="wp-image-51627" width="430" height="259" /></a></figure>
</div>
<div></div>
<p>You can even display that estimate to your site visitors with the new Time To Read Block. (Styling and other customization options are on the way.) </p>
<div>
<figure><img src="https://lh3.googleusercontent.com/7sz_Q2mfe7EEWHdY3W29kOoikXbM7gC-2DI-4ZjhmL6JPJNf_kDQTzaqYZYV5_ccX0BtuVD7fonqN_V2avlS3p2geEG5I9-HvZf9X0zelStupwcdX92_SpJcHPlgzFJlzPsrFBBHxM-4mqUWoot9_w" alt="" /></figure>
</div>
<p><strong><br /></strong><strong>When to use this feature: </strong>You do a lot of longform writing on your site, regularly publishing posts that go over 1,000 words. With this new block, let readers decide whether to start your post now, or wait until they have time to read it in one sitting. It’s a small touch, but a surprisingly thoughtful one.&nbsp;</p>
<h2>Access previous edits to your templates&nbsp;</h2>
<p>Access to previous versions and revisions of posts/pages has long been a staple of the WordPress experience. We’ve now added that same functionality to templates and template parts in the Editor.  </p>
<p>This can be useful for restoring a previous version of a template that you&#8217;ve made changes to, or simply for reviewing the changes you&#8217;ve made over time.&nbsp;</p>
<div>
<figure><img src="https://lh5.googleusercontent.com/pIP-QBRAtdUVor7WiYW6EB0aHlUlwAwZtR2cck44NCulAu62gh2pyh-KYfhivk6gqO-N5Z8G_AZg-GCgSpdZ_MpDqias9H9M6VnBrHyi7hXhSuKldzvQ464WnXv8X5vYFxpidQ_Ucu9rs_X1YLJiqg" alt="" width="475" height="472" /></figure>
</div>
<div></div>
<p><strong>When to use this feature: </strong>You experimented with some stylistic changes to your homepage template, but after a couple weeks of trying it out, the new look just isn’t hitting quite right. Head over to revisions to easily restore the previous version of the template. </p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What’s New on WordPress.com: Focus Mode, Promotion From Your Pocket, and Risk-Free Redesigns</title>
		<link>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-11/</link>
		<comments>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-11/#comments</comments>
		<pubDate>Thu, 20 Apr 2023 20:01:20 +0000</pubDate>
		<dc:creator>Alveare Delle Delizie</dc:creator>
				<category><![CDATA[Friuli Venezia Giulia]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Select Distraction]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-11/</guid>
		<description><![CDATA[At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&#160; The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits&#160;<a href="https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-11/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&nbsp;</p>
<p>The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits to your work are readily accessible.&nbsp;</p>
<p>Let’s jump in and take a look.&nbsp;&nbsp;&nbsp;</p>
<h2>Write without distractions&nbsp;</h2>
<p>One of writing’s biggest challenges has nothing to do with the words — it’s filtering out the noise (both literal and figurative) so you can focus. We can’t change your physical environment, but we’re doing what we can to help you get into “the zone” a little more easily. Enter distraction free mode, a minimalist WordPress experience that removes the buttons and menus.&nbsp;&nbsp;&nbsp;</p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif"><img src="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif?w=1024" alt="" class="wp-image-51617" width="703" height="483" /></a></figure>
</div>
<div></div>
<p>From the top toolbar, click the three-dot menu on the right-hand side. Select “Distraction free” and you’ll notice the top toolbar disappear. (To bring it back, simply hover over the top of the page and it’ll pop back down.) Your pages and posts will be distraction-free until you deselect this mode. </p>
<p><strong>When to use this feature: </strong>You can’t resist experimenting with various settings or styles, but you also really need to get that new post finished. (Believe us, we’ve been there.) With this cleaner interface, you can focus on your words and your words alone.&nbsp;</p>
<h2>Promote your content from your phone&nbsp;</h2>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/advertising">Blaze</a>, our self-serve ad platform, makes it easier than ever to spread the word about whatever it is you’re writing, creating, or selling. Now, you can Blaze on the go with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2023/02/15/switch-to-the-new-jetpack-mobile-app/">the Jetpack app</a>.&nbsp;</p>
<figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg?w=547" alt="" class="wp-image-51618" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg?w=566" alt="" class="wp-image-51620" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg?w=519" alt="" class="wp-image-51619" /></a></figure>
</figure>
<p>From the main dashboard, simply click on the banner that says “Promote your content with Blaze.” You can also promote your work directly from the Posts or Pages menus by clicking “More” or the three-dot menu and selecting “Promote with Blaze.”&nbsp;</p>
<p><strong>When to use this feature: </strong>You’re waiting for take-out at your favorite Indian restaurant. While scrolling the Jetpack app, you notice traffic spiking on one of your recent blog posts — something about it is resonating with readers. Maximize its impact by boosting it to an even wider audience with Blaze.</p>
<h2>Easily find and add free images&nbsp;</h2>
<p>Need a featured image for your post? You can now find and add free-to-use images even faster. Open the Inserter (the red “+” button at the top left of any post or page), navigate to the tab labeled “Media,” click “Openverse,” and find the perfect option from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://openverse.org/">our favorite open-source library</a>. </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png?w=1024" alt="" class="wp-image-51629" width="581" height="220" /></a></figure>
</div>
<div></div>
<p>You can also still access Openverse from the Image Block or Media Library. </p>
<p><strong>When to use this feature: </strong>You’re on the hunt for just the right image to use in your new blog post. Rather than doing a Google search in a separate tab — and running the risk of using an image you don’t have rights to — use the Inserter to search and place exactly what you need.&nbsp;</p>
<h2>Give readers a timetable</h2>
<p>Here’s a fun built-in feature that a lot of folks don’t know about: The WordPress.com editor’s “Outline” function displays a post’s various headings for easy scanning, as well as a word/character count and a “time to read” estimate of how long it would take an average reader to get through the content on that page: </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png?w=694" alt="" class="wp-image-51627" width="430" height="259" /></a></figure>
</div>
<div></div>
<p>You can even display that estimate to your site visitors with the new Time To Read Block. (Styling and other customization options are on the way.) </p>
<div>
<figure><img src="https://lh3.googleusercontent.com/7sz_Q2mfe7EEWHdY3W29kOoikXbM7gC-2DI-4ZjhmL6JPJNf_kDQTzaqYZYV5_ccX0BtuVD7fonqN_V2avlS3p2geEG5I9-HvZf9X0zelStupwcdX92_SpJcHPlgzFJlzPsrFBBHxM-4mqUWoot9_w" alt="" /></figure>
</div>
<p><strong><br /></strong><strong>When to use this feature: </strong>You do a lot of longform writing on your site, regularly publishing posts that go over 1,000 words. With this new block, let readers decide whether to start your post now, or wait until they have time to read it in one sitting. It’s a small touch, but a surprisingly thoughtful one.&nbsp;</p>
<h2>Access previous edits to your templates&nbsp;</h2>
<p>Access to previous versions and revisions of posts/pages has long been a staple of the WordPress experience. We’ve now added that same functionality to templates and template parts in the Editor.  </p>
<p>This can be useful for restoring a previous version of a template that you&#8217;ve made changes to, or simply for reviewing the changes you&#8217;ve made over time.&nbsp;</p>
<div>
<figure><img src="https://lh5.googleusercontent.com/pIP-QBRAtdUVor7WiYW6EB0aHlUlwAwZtR2cck44NCulAu62gh2pyh-KYfhivk6gqO-N5Z8G_AZg-GCgSpdZ_MpDqias9H9M6VnBrHyi7hXhSuKldzvQ464WnXv8X5vYFxpidQ_Ucu9rs_X1YLJiqg" alt="" width="475" height="472" /></figure>
</div>
<div></div>
<p><strong>When to use this feature: </strong>You experimented with some stylistic changes to your homepage template, but after a couple weeks of trying it out, the new look just isn’t hitting quite right. Head over to revisions to easily restore the previous version of the template. </p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What’s New on WordPress.com: Focus Mode, Promotion From Your Pocket, and Risk-Free Redesigns</title>
		<link>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-10/</link>
		<comments>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-10/#comments</comments>
		<pubDate>Thu, 20 Apr 2023 20:01:20 +0000</pubDate>
		<dc:creator>Pani cunzatu</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Select Distraction]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-10/</guid>
		<description><![CDATA[At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&#160; The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits&#160;<a href="https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-10/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&nbsp;</p>
<p>The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits to your work are readily accessible.&nbsp;</p>
<p>Let’s jump in and take a look.&nbsp;&nbsp;&nbsp;</p>
<h2>Write without distractions&nbsp;</h2>
<p>One of writing’s biggest challenges has nothing to do with the words — it’s filtering out the noise (both literal and figurative) so you can focus. We can’t change your physical environment, but we’re doing what we can to help you get into “the zone” a little more easily. Enter distraction free mode, a minimalist WordPress experience that removes the buttons and menus.&nbsp;&nbsp;&nbsp;</p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif"><img src="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif?w=1024" alt="" class="wp-image-51617" width="703" height="483" /></a></figure>
</div>
<div></div>
<p>From the top toolbar, click the three-dot menu on the right-hand side. Select “Distraction free” and you’ll notice the top toolbar disappear. (To bring it back, simply hover over the top of the page and it’ll pop back down.) Your pages and posts will be distraction-free until you deselect this mode. </p>
<p><strong>When to use this feature: </strong>You can’t resist experimenting with various settings or styles, but you also really need to get that new post finished. (Believe us, we’ve been there.) With this cleaner interface, you can focus on your words and your words alone.&nbsp;</p>
<h2>Promote your content from your phone&nbsp;</h2>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/advertising">Blaze</a>, our self-serve ad platform, makes it easier than ever to spread the word about whatever it is you’re writing, creating, or selling. Now, you can Blaze on the go with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2023/02/15/switch-to-the-new-jetpack-mobile-app/">the Jetpack app</a>.&nbsp;</p>
<figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg?w=547" alt="" class="wp-image-51618" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg?w=566" alt="" class="wp-image-51620" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg?w=519" alt="" class="wp-image-51619" /></a></figure>
</figure>
<p>From the main dashboard, simply click on the banner that says “Promote your content with Blaze.” You can also promote your work directly from the Posts or Pages menus by clicking “More” or the three-dot menu and selecting “Promote with Blaze.”&nbsp;</p>
<p><strong>When to use this feature: </strong>You’re waiting for take-out at your favorite Indian restaurant. While scrolling the Jetpack app, you notice traffic spiking on one of your recent blog posts — something about it is resonating with readers. Maximize its impact by boosting it to an even wider audience with Blaze.</p>
<h2>Easily find and add free images&nbsp;</h2>
<p>Need a featured image for your post? You can now find and add free-to-use images even faster. Open the Inserter (the red “+” button at the top left of any post or page), navigate to the tab labeled “Media,” click “Openverse,” and find the perfect option from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://openverse.org/">our favorite open-source library</a>. </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png?w=1024" alt="" class="wp-image-51629" width="581" height="220" /></a></figure>
</div>
<div></div>
<p>You can also still access Openverse from the Image Block or Media Library. </p>
<p><strong>When to use this feature: </strong>You’re on the hunt for just the right image to use in your new blog post. Rather than doing a Google search in a separate tab — and running the risk of using an image you don’t have rights to — use the Inserter to search and place exactly what you need.&nbsp;</p>
<h2>Give readers a timetable</h2>
<p>Here’s a fun built-in feature that a lot of folks don’t know about: The WordPress.com editor’s “Outline” function displays a post’s various headings for easy scanning, as well as a word/character count and a “time to read” estimate of how long it would take an average reader to get through the content on that page: </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png?w=694" alt="" class="wp-image-51627" width="430" height="259" /></a></figure>
</div>
<div></div>
<p>You can even display that estimate to your site visitors with the new Time To Read Block. (Styling and other customization options are on the way.) </p>
<div>
<figure><img src="https://lh3.googleusercontent.com/7sz_Q2mfe7EEWHdY3W29kOoikXbM7gC-2DI-4ZjhmL6JPJNf_kDQTzaqYZYV5_ccX0BtuVD7fonqN_V2avlS3p2geEG5I9-HvZf9X0zelStupwcdX92_SpJcHPlgzFJlzPsrFBBHxM-4mqUWoot9_w" alt="" /></figure>
</div>
<p><strong><br /></strong><strong>When to use this feature: </strong>You do a lot of longform writing on your site, regularly publishing posts that go over 1,000 words. With this new block, let readers decide whether to start your post now, or wait until they have time to read it in one sitting. It’s a small touch, but a surprisingly thoughtful one.&nbsp;</p>
<h2>Access previous edits to your templates&nbsp;</h2>
<p>Access to previous versions and revisions of posts/pages has long been a staple of the WordPress experience. We’ve now added that same functionality to templates and template parts in the Editor.  </p>
<p>This can be useful for restoring a previous version of a template that you&#8217;ve made changes to, or simply for reviewing the changes you&#8217;ve made over time.&nbsp;</p>
<div>
<figure><img src="https://lh5.googleusercontent.com/pIP-QBRAtdUVor7WiYW6EB0aHlUlwAwZtR2cck44NCulAu62gh2pyh-KYfhivk6gqO-N5Z8G_AZg-GCgSpdZ_MpDqias9H9M6VnBrHyi7hXhSuKldzvQ464WnXv8X5vYFxpidQ_Ucu9rs_X1YLJiqg" alt="" width="475" height="472" /></figure>
</div>
<div></div>
<p><strong>When to use this feature: </strong>You experimented with some stylistic changes to your homepage template, but after a couple weeks of trying it out, the new look just isn’t hitting quite right. Head over to revisions to easily restore the previous version of the template. </p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What’s New on WordPress.com: Focus Mode, Promotion From Your Pocket, and Risk-Free Redesigns</title>
		<link>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-9/</link>
		<comments>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-9/#comments</comments>
		<pubDate>Thu, 20 Apr 2023 20:01:20 +0000</pubDate>
		<dc:creator>Ragazze conTorte</dc:creator>
				<category><![CDATA[Lazio]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Select Distraction]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-9/</guid>
		<description><![CDATA[At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&#160; The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits&#160;<a href="https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-9/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&nbsp;</p>
<p>The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits to your work are readily accessible.&nbsp;</p>
<p>Let’s jump in and take a look.&nbsp;&nbsp;&nbsp;</p>
<h2>Write without distractions&nbsp;</h2>
<p>One of writing’s biggest challenges has nothing to do with the words — it’s filtering out the noise (both literal and figurative) so you can focus. We can’t change your physical environment, but we’re doing what we can to help you get into “the zone” a little more easily. Enter distraction free mode, a minimalist WordPress experience that removes the buttons and menus.&nbsp;&nbsp;&nbsp;</p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif"><img src="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif?w=1024" alt="" class="wp-image-51617" width="703" height="483" /></a></figure>
</div>
<div></div>
<p>From the top toolbar, click the three-dot menu on the right-hand side. Select “Distraction free” and you’ll notice the top toolbar disappear. (To bring it back, simply hover over the top of the page and it’ll pop back down.) Your pages and posts will be distraction-free until you deselect this mode. </p>
<p><strong>When to use this feature: </strong>You can’t resist experimenting with various settings or styles, but you also really need to get that new post finished. (Believe us, we’ve been there.) With this cleaner interface, you can focus on your words and your words alone.&nbsp;</p>
<h2>Promote your content from your phone&nbsp;</h2>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/advertising">Blaze</a>, our self-serve ad platform, makes it easier than ever to spread the word about whatever it is you’re writing, creating, or selling. Now, you can Blaze on the go with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2023/02/15/switch-to-the-new-jetpack-mobile-app/">the Jetpack app</a>.&nbsp;</p>
<figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg?w=547" alt="" class="wp-image-51618" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg?w=566" alt="" class="wp-image-51620" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg?w=519" alt="" class="wp-image-51619" /></a></figure>
</figure>
<p>From the main dashboard, simply click on the banner that says “Promote your content with Blaze.” You can also promote your work directly from the Posts or Pages menus by clicking “More” or the three-dot menu and selecting “Promote with Blaze.”&nbsp;</p>
<p><strong>When to use this feature: </strong>You’re waiting for take-out at your favorite Indian restaurant. While scrolling the Jetpack app, you notice traffic spiking on one of your recent blog posts — something about it is resonating with readers. Maximize its impact by boosting it to an even wider audience with Blaze.</p>
<h2>Easily find and add free images&nbsp;</h2>
<p>Need a featured image for your post? You can now find and add free-to-use images even faster. Open the Inserter (the red “+” button at the top left of any post or page), navigate to the tab labeled “Media,” click “Openverse,” and find the perfect option from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://openverse.org/">our favorite open-source library</a>. </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png?w=1024" alt="" class="wp-image-51629" width="581" height="220" /></a></figure>
</div>
<div></div>
<p>You can also still access Openverse from the Image Block or Media Library. </p>
<p><strong>When to use this feature: </strong>You’re on the hunt for just the right image to use in your new blog post. Rather than doing a Google search in a separate tab — and running the risk of using an image you don’t have rights to — use the Inserter to search and place exactly what you need.&nbsp;</p>
<h2>Give readers a timetable</h2>
<p>Here’s a fun built-in feature that a lot of folks don’t know about: The WordPress.com editor’s “Outline” function displays a post’s various headings for easy scanning, as well as a word/character count and a “time to read” estimate of how long it would take an average reader to get through the content on that page: </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png?w=694" alt="" class="wp-image-51627" width="430" height="259" /></a></figure>
</div>
<div></div>
<p>You can even display that estimate to your site visitors with the new Time To Read Block. (Styling and other customization options are on the way.) </p>
<div>
<figure><img src="https://lh3.googleusercontent.com/7sz_Q2mfe7EEWHdY3W29kOoikXbM7gC-2DI-4ZjhmL6JPJNf_kDQTzaqYZYV5_ccX0BtuVD7fonqN_V2avlS3p2geEG5I9-HvZf9X0zelStupwcdX92_SpJcHPlgzFJlzPsrFBBHxM-4mqUWoot9_w" alt="" /></figure>
</div>
<p><strong><br /></strong><strong>When to use this feature: </strong>You do a lot of longform writing on your site, regularly publishing posts that go over 1,000 words. With this new block, let readers decide whether to start your post now, or wait until they have time to read it in one sitting. It’s a small touch, but a surprisingly thoughtful one.&nbsp;</p>
<h2>Access previous edits to your templates&nbsp;</h2>
<p>Access to previous versions and revisions of posts/pages has long been a staple of the WordPress experience. We’ve now added that same functionality to templates and template parts in the Editor.  </p>
<p>This can be useful for restoring a previous version of a template that you&#8217;ve made changes to, or simply for reviewing the changes you&#8217;ve made over time.&nbsp;</p>
<div>
<figure><img src="https://lh5.googleusercontent.com/pIP-QBRAtdUVor7WiYW6EB0aHlUlwAwZtR2cck44NCulAu62gh2pyh-KYfhivk6gqO-N5Z8G_AZg-GCgSpdZ_MpDqias9H9M6VnBrHyi7hXhSuKldzvQ464WnXv8X5vYFxpidQ_Ucu9rs_X1YLJiqg" alt="" width="475" height="472" /></figure>
</div>
<div></div>
<p><strong>When to use this feature: </strong>You experimented with some stylistic changes to your homepage template, but after a couple weeks of trying it out, the new look just isn’t hitting quite right. Head over to revisions to easily restore the previous version of the template. </p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What’s New on WordPress.com: Focus Mode, Promotion From Your Pocket, and Risk-Free Redesigns</title>
		<link>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-8/</link>
		<comments>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-8/#comments</comments>
		<pubDate>Thu, 20 Apr 2023 20:01:20 +0000</pubDate>
		<dc:creator>ricette da coinquiline</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Select Distraction]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-8/</guid>
		<description><![CDATA[At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&#160; The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits&#160;<a href="https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-8/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>At WordPress.com, we’re always pushing our platform to do even more so that you can create, design, and publish amazing things with ease.&nbsp;</p>
<p>The last few weeks have found us as busy as the springtime bees improving the writing and publishing process even more, bringing our ad platform to mobile, and ensuring that previous edits to your work are readily accessible.&nbsp;</p>
<p>Let’s jump in and take a look.&nbsp;&nbsp;&nbsp;</p>
<h2>Write without distractions&nbsp;</h2>
<p>One of writing’s biggest challenges has nothing to do with the words — it’s filtering out the noise (both literal and figurative) so you can focus. We can’t change your physical environment, but we’re doing what we can to help you get into “the zone” a little more easily. Enter distraction free mode, a minimalist WordPress experience that removes the buttons and menus.&nbsp;&nbsp;&nbsp;</p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif"><img src="https://en-blog.files.wordpress.com/2023/04/2023-04-05-14.45.15.gif?w=1024" alt="" class="wp-image-51617" width="703" height="483" /></a></figure>
</div>
<div></div>
<p>From the top toolbar, click the three-dot menu on the right-hand side. Select “Distraction free” and you’ll notice the top toolbar disappear. (To bring it back, simply hover over the top of the page and it’ll pop back down.) Your pages and posts will be distraction-free until you deselect this mode. </p>
<p><strong>When to use this feature: </strong>You can’t resist experimenting with various settings or styles, but you also really need to get that new post finished. (Believe us, we’ve been there.) With this cleaner interface, you can focus on your words and your words alone.&nbsp;</p>
<h2>Promote your content from your phone&nbsp;</h2>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/advertising">Blaze</a>, our self-serve ad platform, makes it easier than ever to spread the word about whatever it is you’re writing, creating, or selling. Now, you can Blaze on the go with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2023/02/15/switch-to-the-new-jetpack-mobile-app/">the Jetpack app</a>.&nbsp;</p>
<figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9088.jpeg?w=547" alt="" class="wp-image-51618" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9089.jpeg?w=566" alt="" class="wp-image-51620" /></a></figure>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg"><img src="https://en-blog.files.wordpress.com/2023/04/img_9090.jpeg?w=519" alt="" class="wp-image-51619" /></a></figure>
</figure>
<p>From the main dashboard, simply click on the banner that says “Promote your content with Blaze.” You can also promote your work directly from the Posts or Pages menus by clicking “More” or the three-dot menu and selecting “Promote with Blaze.”&nbsp;</p>
<p><strong>When to use this feature: </strong>You’re waiting for take-out at your favorite Indian restaurant. While scrolling the Jetpack app, you notice traffic spiking on one of your recent blog posts — something about it is resonating with readers. Maximize its impact by boosting it to an even wider audience with Blaze.</p>
<h2>Easily find and add free images&nbsp;</h2>
<p>Need a featured image for your post? You can now find and add free-to-use images even faster. Open the Inserter (the red “+” button at the top left of any post or page), navigate to the tab labeled “Media,” click “Openverse,” and find the perfect option from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://openverse.org/">our favorite open-source library</a>. </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-05-at-1.10.48-pm.png?w=1024" alt="" class="wp-image-51629" width="581" height="220" /></a></figure>
</div>
<div></div>
<p>You can also still access Openverse from the Image Block or Media Library. </p>
<p><strong>When to use this feature: </strong>You’re on the hunt for just the right image to use in your new blog post. Rather than doing a Google search in a separate tab — and running the risk of using an image you don’t have rights to — use the Inserter to search and place exactly what you need.&nbsp;</p>
<h2>Give readers a timetable</h2>
<p>Here’s a fun built-in feature that a lot of folks don’t know about: The WordPress.com editor’s “Outline” function displays a post’s various headings for easy scanning, as well as a word/character count and a “time to read” estimate of how long it would take an average reader to get through the content on that page: </p>
<div>
<figure><a target="_blank" rel="nofollow" href="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png"><img src="https://en-blog.files.wordpress.com/2023/04/screen-shot-2023-04-06-at-9.30.44-am-1.png?w=694" alt="" class="wp-image-51627" width="430" height="259" /></a></figure>
</div>
<div></div>
<p>You can even display that estimate to your site visitors with the new Time To Read Block. (Styling and other customization options are on the way.) </p>
<div>
<figure><img src="https://lh3.googleusercontent.com/7sz_Q2mfe7EEWHdY3W29kOoikXbM7gC-2DI-4ZjhmL6JPJNf_kDQTzaqYZYV5_ccX0BtuVD7fonqN_V2avlS3p2geEG5I9-HvZf9X0zelStupwcdX92_SpJcHPlgzFJlzPsrFBBHxM-4mqUWoot9_w" alt="" /></figure>
</div>
<p><strong><br /></strong><strong>When to use this feature: </strong>You do a lot of longform writing on your site, regularly publishing posts that go over 1,000 words. With this new block, let readers decide whether to start your post now, or wait until they have time to read it in one sitting. It’s a small touch, but a surprisingly thoughtful one.&nbsp;</p>
<h2>Access previous edits to your templates&nbsp;</h2>
<p>Access to previous versions and revisions of posts/pages has long been a staple of the WordPress experience. We’ve now added that same functionality to templates and template parts in the Editor.  </p>
<p>This can be useful for restoring a previous version of a template that you&#8217;ve made changes to, or simply for reviewing the changes you&#8217;ve made over time.&nbsp;</p>
<div>
<figure><img src="https://lh5.googleusercontent.com/pIP-QBRAtdUVor7WiYW6EB0aHlUlwAwZtR2cck44NCulAu62gh2pyh-KYfhivk6gqO-N5Z8G_AZg-GCgSpdZ_MpDqias9H9M6VnBrHyi7hXhSuKldzvQ464WnXv8X5vYFxpidQ_Ucu9rs_X1YLJiqg" alt="" width="475" height="472" /></figure>
</div>
<div></div>
<p><strong>When to use this feature: </strong>You experimented with some stylistic changes to your homepage template, but after a couple weeks of trying it out, the new look just isn’t hitting quite right. Head over to revisions to easily restore the previous version of the template. </p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/whats-new-on-wordpress-com-focus-mode-promotion-from-your-pocket-and-risk-free-redesigns-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- Wp Fastest Cache: XML Content -->