Skip to content

WordPress 7.2 lets the Interactivity API render HTML safely

A new data-wp-html directive paired with asDangerousHTML() replaces the old DIY innerHTML workaround, and the router gains navigation-state tracking. WordPress 7.2 ships 8 December.

Source: WordPress Core

WordPress published the WordPress 7.2 Interactivity API dev notes today, 17 November, alongside the release’s first Release Candidate. The headline addition is a data-wp-html directive: data-wp-text has always set an element’s text from the store, but there was no equivalent for markup, so showing HTML meant writing your own innerHTML assignment inside a data-wp-watch callback, outside the API’s own rendering.

data-wp-html only accepts values wrapped in a new asDangerousHTML() function. The function does not sanitise anything, it carries the same responsibility as React’s dangerouslySetInnerHTML, and the value it returns is opaque: it is trusted by identity, not content, so copying it into context or serialising it to JSON strips the trust. The router also gains state.navigating and state.initiator, replacing the deprecated state.navigation.hasStarted and hasFinished from WordPress 7.0, so a block can show a loading state only for navigations it started itself.

<div data-wp-html="state.trustedMarkup"></div>

Why it matters: if you build blocks or themes on the Interactivity API, you now get a sanctioned way to render trusted HTML from the store instead of reaching outside the framework for it, and the new navigation flags remove a common source of loading-indicator bugs. WordPress 7.2 reaches general release on 8 December.

The caveat: 7.2 also removes three previously deprecated behaviours outright, including the data-wp-ignore directive, so code still relying on them will break.

Share

Related reading

More from The Wire

All briefs