Support » Developing with WordPress » Gutenberg: block patterns missing when use allowed_block_types_all filter wp 5.8

  • Resolved kworrall

    (@kworrall)


    Hi,

    I have been successfully using the allowed_block_types filter on a few projects over the past year or so, in order to restrict blocks for clients.

    It has been working fine and is still working OK for the purpose of allowing only certain blocks.

    However, on a recent WordPress update (5.8 I think) using this filter has stopped my registered block patterns from showing in the editor.

    I have looked as best I can in the release notes, google and forums – I notice that the filter is deprecated and replaced with allowed_block_types_all but that doesn’t help.

    I can also confirm that it happens on clean twentytwentyone install with the following example test code:

    function filter_allowed_block_types_when_post_provided( $allowed_block_types, $editor_context ) {
    	if ( ! empty( $editor_context->post ) ) {
    		return array(
    			'core/group',
    			'core/image',
    			'core/paragraph',
    			'core/columns',
    			'core/heading',
    			'core/list',
    			'core/gallery',
    			'core/quote',
    			'core/video',
    			'core/pullquote',
    			'core/media-text',
    			'core/separator',
    			'core/search',
    			'core/embed',
    			'core/spacer',
    			'core-embed/twitter',
    			'core-embed/youtube',
    			'core-embed/facebook',
    			'core-embed/instagram',
    			'core/table',
    			'core/html',
    			'core/shortcode',				
    		);
    	}
    	return $allowed_block_types;
    }
    add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_when_post_provided', 10, 2 );
    
    function wpdocs_register_my_patterns() {
    	
    	register_block_pattern(
    		'wpdocs-my-plugin/my-awesome-pattern',
    		array(
    			'title'       => __( 'Two buttons TEST', 'wpdocs-my-plugin' ),
    			'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
    			'content'     => "<!-- wp:buttons {\"align\":\"center\"} -->\n<div class=\"wp-block-buttons aligncenter\"><!-- wp:button {\"backgroundColor\":\"very-dark-gray\",\"borderRadius\":0} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius\">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"very-dark-gray\",\"borderRadius\":0,\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius\">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
    		)
    	);
    
    }
    add_action( 'init', 'wpdocs_register_my_patterns' );

    Would appreciate if anyone can help or has advice?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Your block pattern is using the core/buttons and core/button blocks, but these are not in your list of allowed blocks. Block patterns will only appear if the blocks in them are allowed.

    Thread Starter kworrall

    (@kworrall)

    Thanks for your help @jakept

    Oops yes I missed adding the blocks into the example above although had checked and double checked the blocks being enabled on my project (different than simple example I’d thrown together above) – however you’ve given me the answer to my issue…

    Basically my block pattern was using columns, and I’d got the ‘core/columns’ block enabled but not ‘core/column’ block also in the allowed blocks filter. I didn’t realise both were required as the columns block appears to works fine with only ‘core/columns’ enabled.

    • This reply was modified 2 days, 9 hours ago by kworrall.
    • This reply was modified 2 days, 9 hours ago by kworrall.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.