I'm testing the new Interactivity API and want build a block that renders the title and content of a random post.
This is the content of the render.php file:
<?php $block_attributes = get_block_wrapper_attributes() ?> <div <?php echo $block_attributes; ?> data-wp-interactive="mystore" <?php echo wp_interactivity_data_wp_context( array( 'isOpen' => false ) ); ?> > <button data-wp-on--click="actions.getRandomPost">Show me a random post</button> <p data-wp-bind--hidden="!context.isOpen"> <span data-wp-text="context.randomPost.title.rendered"></span> <div data-wp-text="context.randomPost.content.rendered"></div> </p> </div> This is the content of the view.js file:
import { store, getContext } from '@wordpress/interactivity' store( 'mystore', { actions: { *getRandomPost() { // a generator function const context = getContext() context.isOpen = ! context.isOpen if ( context.isOpen ) { const posts = yield fetch( 'http://' + window.location.hostname + '/wp-json/wp/v2/posts' ).then( function( response ){ return response.json() } ) context.randomPost = posts[ ( Math.floor( Math.random() * posts.length ) ) ] } }, } } ) The issue is that the post content (context.randomPost.content.rendered) is displayed as raw HTML, without being properly interpreted as formatted HTML.
Example:
<p>Hello world! Welcome to <strong>WordPress</strong>. This is your first post. Edit or delete it, then start writing!</p> Is there a way to display HTML content using the wp-text directive? Is there a specific directive to render HTML content?