Filter Gutenberg Blocks Content

Have you tried using parse_blocks() (pass in get_the_content())? That returns an array of all the blocks in your content. From there you can pull out block names and id attributes using an array map or foreach. Assuming you only want to pull anchor tags from the headings, you could do something like: $blocks = parse_blocks( …

Read more

Gutenberg First Block on Page Conditional?

Here’s a solution I’ve come up with, that works with WordPress 5.4 and ACF Pro 5.8.9. First you need this function somewhere in functions.php: /** * Get ID of the first ACF block on the page */ function sg_get_first_block_id() { $post = get_post(); if(has_blocks($post->post_content)) { $blocks = parse_blocks($post->post_content); $first_block_attrs = $blocks[0][‘attrs’]; if(array_key_exists(‘id’, $first_block_attrs)) { return …

Read more

Your site doesn’t include support for the block… after registering a block

The js-file is not executed on time By the time the DOM load event is executed the block editor has already parsed the post content and at that time the block wasn’t yet registered so it tells you that it doesn’t exist. Instead of: window.addEventListener( ‘load’, () => { mcm_register_menu_card_section_block(); }); try: wp.domReady(mcm_register_menu_card_section_block); and in …

Read more

Wrapping Gutenberg Blocks with a div/section/article

Use ‘Groups’: If you do not see the ‘Group’ option, like me, you’ll have to update WP. After that, simply create a group, then add new blocks or drag existing blocks into it. Select the group and add HTML Anchors and classes via the ‘Advanced’ tab of the block’s menu.