admin管理员组

文章数量:1384181

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?

Share Improve this question asked May 10, 2020 at 14:34 Siddharth ThevarilSiddharth Thevaril 5777 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I found the solution.

addFilter( 'blocks.registerBlockType ) needs to be called before the call to registerBlockType.

本文标签: hooksHow to extend custom (noncore) blocks