Custom patterns using Cover block not working (unexpected or invalid message)
If I add a custom pattern that utilizes the cover block, like so:
function my_custom_wp_block_patterns() {
register_block_pattern_category(
'headers', array( 'label' => __( 'Headers', 'nrdly-sierra' ) )
);
// Full height cover, heading and buttons
register_block_pattern (
'nrdly-sierra/nrdly-hero-pattern',
array(
'title' => __( 'Full height cover, heading and buttons', 'nrdly-sierra' ),
'categories' => array('headers'),
'keywords' => array('cover','image'),
'content' => "<!-- wp:cover {\"url\":\"https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1\",\"id\":1011,\"dimRatio\":0,\"align\":\"full\",\"className\":\"hero\"} -->\n<div class=\"wp-block-cover alignfull hero\"><img class=\"wp-block-cover__image-background wp-image-1011\" alt=\"\" src=\"https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1\" data-object-fit=\"cover\"/><div class=\"wp-block-cover__inner-container\"><!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:heading {\"level\":1} -->\n<h1>Landscape.</h1>\n<!-- /wp:heading -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"textColor\":\"white\",\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-white-color has-text-color\">Take action</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"white\",\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-white-color has-text-color\">Learn more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column -->\n\n<!-- wp:column -->\n<div class=\"wp-block-column\"></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns --></div></div>\n<!-- /wp:cover -->",
)
);
}
add_action( 'init', 'my_custom_wp_block_patterns', 99 );
The pattern preview in the insert panel looks ok in my initial local dev.
However, as soon as I add my theme to another site, the preview (and inserted) pattern shows the "this block contains unexpected or invalid content" message.
If I click to attempt recovery, it works. But this is not a valid / good solution as my theme users will have no idea what they are attempting to recover without seeing the preview.
If I click "convert to HTML", I get the following:
<div class="wp-block-cover alignfull hero"><img class="wp-block-cover__image-background wp-image-1011" alt="" src="https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1" data-object-fit="cover"/><div class="wp-block-cover__inner-container"></div></div>
Which is obviously missing the actual inner content of the block.
If I click "resolve", I get:
<dfiv class="wp-block-cover alignfull hrero"><img class="wp-block-cover__image-background wp-"><image-1011" alt="" src="https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1" dalt="" class="wp-objimagect-1011"/></fitgure><div class="wp-block-cover alignfull hero"/><div class="wp-block-cover__inner-container"></div></div>
Happy to provide more info if it'll be useful. I'll attach a few screenshots too. This happens to ALL custom patterns I create using the cover block within the pattern. All of the other custom patterns I create work well.
Thanks for taking a look!
@bradley2083 It looks like the markup for your cover block isn't right. Probably from all the escaping.
I would recommend using single quotes for the content of your cover block, so that you can use double quotes normally within the string.
There's some examples here: https://github.com/WordPress/gutenberg/blob/e0c219064836c77fe69845c56575ca007e6038ef/lib/block-patterns.php#L9-L25
I tested using my own cover block and didn't have any issues with this approach. Here's an example of the code I used:
register_block_pattern(
'gutenberg',
array(
'title' => __( 'Fancy cover', 'gutenberg' ),
'categories' => array( 'header' ),
'keywords' => array( 'cover', 'image' ),
'content' => '<!-- wp:cover {"url":"http://localhost:8888/wp-content/uploads/2021/02/percy.jpg","id":134} -->
<div class="wp-block-cover has-background-dim"><img class="wp-block-cover__image-background wp-image-134" alt="" src="http://localhost:8888/wp-content/uploads/2021/02/percy.jpg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:buttons {"contentJustification":"center"} -->
<div class="wp-block-buttons is-content-justification-center"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link">Yes</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div></div>
<!-- /wp:cover -->',
)
);
@bradley2083
If the pattern is to be used across different sites, try removing reference to the image ID.
So that would be e.g. "id":1011 and wp-image-1011 in your code.
@talldan Thanks for the reply and sorry for the delayed response. The escaping was directly copied when I escaped using this tool which seems to be the go-to tool to escape JSON. Any opinion on that?
Also, the patterns and photos all work properly locally but not in a live environment.
I've tested with the escaping removed, single quotes and no image ID and same issue. Occurs only in production environment and not locally.
Updated example:
register_block_pattern (
'nrdly-sierra/nrdly-hero-pattern',
array(
'title' => __( 'Full height cover, heading and buttons', 'nrdly-sierra' ),
'categories' => array('headers'),
'keywords' => array('cover','image'),
'content' => '
<!-- wp:cover {"url":"https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1","dimRatio":0,"align":"full","className":"hero"} -->
<div class="wp-block-cover alignfull hero"><img class="wp-block-cover__image-background" alt="" src="https://www.dropbox.com/s/ko7su6thbibwpe4/c86b8baa.jpeg?raw=1" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:heading {"level":1} -->
<h1>Be a hero.</h1>
<!-- /wp:heading -->
<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"textColor":"white","className":"is-style-outline"} -->
<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-white-color has-text-color">Take action</a></div>
<!-- /wp:button -->
<!-- wp:button {"textColor":"white","className":"is-style-outline"} -->
<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-white-color has-text-color">Learn more</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"></div>
<!-- /wp:column --></div>
<!-- /wp:columns --></div></div>
<!-- /wp:cover -->',
)
);
It appears that the wp-columns-block is where it's breaking in my production environment. Can you test that local vs. production with a pattern on your end that uses the cover block with columns inside?
Hey, there. Just hoping you can re-open this ticket as it has not been resolved and is still an active issue I'm seeing. It could be that this is specific to the types of blocks I'm using within my pattern. It could have to do with a local install treating things one way and production another. Thank you!