I created few custom blocks which work just fine.
I plan for these blocks to have common attributes and I'm using the blocks.registerBlockType
filter to add an attribute, here is my code:
import { blocksWithSharedAttributes } from '../blocks-wth-shared-attributes.jsx'; // an array of names of custom blocks.
const { addFilter } = wp.hooks;
const addToggleRenderBlockAttribute = ( settings, name ) => {
if ( ! blocksWithSharedAttributes.includes( name ) ) {
return settings; // This callback returns here. :(
}
settings.attributes = Object.assign( settings.attributes, {
toggleBlockRender: {
type: 'boolean',
default: false,
},
} );
return settings;
};
addFilter( 'blocks.registerBlockType', 'someNamespace/attribute/toggleBlockRender', addToggleRenderBlockAttribute );
The problem I'm facing is that the name
argument doesn't have any of the custom blocks I created and due to that the callback returns at line 7.
The name
argument only has core/
blocks. How can I make custom blocks appear in the filter?
I created few custom blocks which work just fine.
I plan for these blocks to have common attributes and I'm using the blocks.registerBlockType
filter to add an attribute, here is my code:
import { blocksWithSharedAttributes } from '../blocks-wth-shared-attributes.jsx'; // an array of names of custom blocks.
const { addFilter } = wp.hooks;
const addToggleRenderBlockAttribute = ( settings, name ) => {
if ( ! blocksWithSharedAttributes.includes( name ) ) {
return settings; // This callback returns here. :(
}
settings.attributes = Object.assign( settings.attributes, {
toggleBlockRender: {
type: 'boolean',
default: false,
},
} );
return settings;
};
addFilter( 'blocks.registerBlockType', 'someNamespace/attribute/toggleBlockRender', addToggleRenderBlockAttribute );
The problem I'm facing is that the name
argument doesn't have any of the custom blocks I created and due to that the callback returns at line 7.
The name
argument only has core/
blocks. How can I make custom blocks appear in the filter?
1 Answer
Reset to default 1I found the solution.
addFilter( 'blocks.registerBlockType )
needs to be called before the call to registerBlockType
.