How to display post content in the block editor

It is true that using RawHTML (or dangerouslySetInnerHTML) is discouraged and indeed prone to XSS attacks like <img src=x onerror=alert(1)//> and <p>abc<iframe//src=jAva&Tab;script:alert(3)>def</p>. But if you need to set HTML directly from React, then you’ll have to use dangerouslySetInnerHTML, either directly (e.g. <div dangerouslySetInnerHTML={ ({ __html: ‘foo <b>bar</b>’ }) } />) or via RawHTML in Gutenberg … Read more

Gutenberg: Heading with subheading/paragraph

In this case it seems a block pattern might suffice. The Group block has a templateLock attribute which can be set to all to prevent any additional blocks from being inserted within and also keep the Heading and Paragraph blocks from being moved or deleted. Here’s an example with the template lock and a custom … Read more

How to only load css for used blocks on frontend

To get separate files for each block you’ll need to bring your own webpack config One as simple as this should work: const defaultConfig = require(‘@wordpress/scripts/config/webpack.config’); module.exports = { …defaultConfig, entry: { ‘hero’: ‘./src/hero’, ‘call-to-action’: ‘./src/callToAction’, }, }; Ryan Welcher has an example repo and a video walk-through of its creation (that link is to … Read more

Looping over wordpress meta to create “ ‘s?

Does select(“core/editor”).getEditedPostAttribute(“meta”) get the whole post meta? Only the meta registered using register_post_meta() or register_meta() with the show_in_rest argument set to true. So for example, the meta can be registered like so: register_post_meta( ‘post’, ‘_postcode_pricing_postal_town’, array( ‘show_in_rest’ => true, ‘single’ => true, ‘type’ => ‘string’, // I set this because the meta is *protected*, i.e. … Read more