最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - How to extend custom (non-core) blocks?

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论