css - Custom gutenberg block not rendering with styles on frontend

I've registered a custom block that's basically a wrapper for other content with some background color options

I've registered a custom block that's basically a wrapper for other content with some background color options.

Here's my registration code on the backend:

function mytheme_register_box_block() {
    register_block_type( 'mytheme/box', array(
        'attributes' => array(
            'className' => array(
                'type' => 'string',
                'default' => '',
            ),
        ),
        'supports' => array(
            'customClassName' => false,
        ),
        'editor_script' => 'mytheme-editor',
    ) );
}
add_action( 'init', 'mytheme_register_box_block' );

Here is the code for my block that's imported by my editor script:

const { InnerBlocks } = wp.editor;

export default {
    title: 'Box',
    description: 'Wrap content within an area to apply a colorscheme to.',
    icon: 'grid-view',
    category: 'layout',
    attributes: {
        className: {
            type: 'string',
        },
    },
    styles: [
        {
            name: 'default',
            label: 'Grey',
            isDefault: true,
        },
        {
            name: 'green',
            label: 'Green',
        },
        {
            name: 'orange',
            label: 'Orange',
        },
        {
            name: 'blue',
            label: 'Blue',
        },
        {
            name: 'cyan',
            label: 'Cyan',
        },
    ],
    edit: props => {
        return (
            <div className={ props.className }>
                { ( typeof props.insertBlocksAfter !== 'undefined' ) ? <InnerBlocks /> : <div>Lorem ipsum dolor sit amet.</div> }
            </div>
        )
    },
    save: ( { className } ) => {
        return (
            <div className={ className }>
                <InnerBlocks.Content />
            </div>
        )
    },
};

In Gutenberg the block renders fine and applies the styling, but when it's printed out on the frontend, it's simply rendered without the styling applied.

<div class="wp-block-mytheme-box">...</div>

The attributes list I have in the register_block_type call I just added to try and get it to work, but to no avail. I'm not sure what I'm missing.

I've registered a custom block that's basically a wrapper for other content with some background color options.

Here's my registration code on the backend:

function mytheme_register_box_block() {
    register_block_type( 'mytheme/box', array(
        'attributes' => array(
            'className' => array(
                'type' => 'string',
                'default' => '',
            ),
        ),
        'supports' => array(
            'customClassName' => false,
        ),
        'editor_script' => 'mytheme-editor',
    ) );
}
add_action( 'init', 'mytheme_register_box_block' );

Here is the code for my block that's imported by my editor script:

const { InnerBlocks } = wp.editor;

export default {
    title: 'Box',
    description: 'Wrap content within an area to apply a colorscheme to.',
    icon: 'grid-view',
    category: 'layout',
    attributes: {
        className: {
            type: 'string',
        },
    },
    styles: [
        {
            name: 'default',
            label: 'Grey',
            isDefault: true,
        },
        {
            name: 'green',
            label: 'Green',
        },
        {
            name: 'orange',
            label: 'Orange',
        },
        {
            name: 'blue',
            label: 'Blue',
        },
        {
            name: 'cyan',
            label: 'Cyan',
        },
    ],
    edit: props => {
        return (
            <div className={ props.className }>
                { ( typeof props.insertBlocksAfter !== 'undefined' ) ? <InnerBlocks /> : <div>Lorem ipsum dolor sit amet.</div> }
            </div>
        )
    },
    save: ( { className } ) => {
        return (
            <div className={ className }>
                <InnerBlocks.Content />
            </div>
        )
    },
};

In Gutenberg the block renders fine and applies the styling, but when it's printed out on the frontend, it's simply rendered without the styling applied.

<div class="wp-block-mytheme-box">...</div>

The attributes list I have in the register_block_type call I just added to try and get it to work, but to no avail. I'm not sure what I'm missing.

Share Improve this question asked Jun 7, 2019 at 21:17 Doug WollisonDoug Wollison 3761 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

From what I can tell, registering it with 'customClassName' => false in the supports array in register_block_type prevents the actual style class from being saved to the raw text of the page. I had assumed it would dynamically insert it during the_content when it parses the blocks. In fact there's no point in me registering it on the PHP end since it's all edited and compiled via javascript.

Still, now I'm stuck with allowing custom class names on this block, though I could always just hide that panel in CSS like I am with the Text Settings (because I can opt out of custom/preset font sizes but not drop caps for some reason).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745405214a4626283.html

相关推荐

  • css - Custom gutenberg block not rendering with styles on frontend

    I've registered a custom block that's basically a wrapper for other content with some background color options

    13小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信