How can I filter block registration based on post-type? (Block alignment settings)

I want to disable the .alignwide and .alignfull block alignment options (remove them from the editor UI) on the ‘post’ post-type only.

I have tried using this block registration filter to modify the ‘supports’ attribute:

function disablePostAlignwide( settings, name ) {
    if ( "post" != wp.data.select('core/editor').getCurrentPostType() ) {
        return settings;
    }
    return lodash.assign( {}, settings, {
        supports: lodash.assign( {}, settings.supports, {
            align: ['left', 'center', 'right'],
            alignWide: false,
        } ),
    } );
}
wp.hooks.addFilter( 'blocks.registerBlockType', 'my-namespace', disablePostAlignwide );

This does not work because wp.data.select('core/editor').getCurrentPostType() returns null. I have tried adding the hook in wp.domReady, but then the filter does not run at all, presumably because the hook has already fired.

How can I get the current post-type before the registerBlockType hook fires?

An answer that disables the alignment options on a post-type basis via another method (i.e. PHP) would also be acceptable.

0

Leave a Comment