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

Pre-set CSS class into custom blocktemplate

programmeradmin2浏览0评论

I am trying & learning to build custom block/template for Gutenberg and would like to "pre-set" CSS class for some specific block. Is it possible to add it like below?

Refer: [Template and Block][1]

For example, defined CSS class for one of the heading blockHeadStyle:

function myplugin_register_book_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Books',
        'show_in_rest' => true,
        'template' => array(
            array( 'core/image', array(
                'align' => 'left',
            ) ),
            array( 'core/heading', array(
                'placeholder' => 'Add Author...',
                'class' => 'blockHeadStyle'
            ) ),
            array( 'core/paragraph', array(
                'placeholder' => 'Add Description...',
            )),
        ),
    );
    register_post_type('book', $args);
}
add_action('init', 'myplugin_register_book_post_type');

I am trying & learning to build custom block/template for Gutenberg and would like to "pre-set" CSS class for some specific block. Is it possible to add it like below?

Refer: [Template and Block][1]

For example, defined CSS class for one of the heading blockHeadStyle:

function myplugin_register_book_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Books',
        'show_in_rest' => true,
        'template' => array(
            array( 'core/image', array(
                'align' => 'left',
            ) ),
            array( 'core/heading', array(
                'placeholder' => 'Add Author...',
                'class' => 'blockHeadStyle'
            ) ),
            array( 'core/paragraph', array(
                'placeholder' => 'Add Description...',
            )),
        ),
    );
    register_post_type('book', $args);
}
add_action('init', 'myplugin_register_book_post_type');
Share Improve this question asked Apr 29, 2019 at 9:05 kevinckckevinckc 731 silver badge7 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 5

In case anyone is looking for an answer to this that doesn't involve adding a filter and a bunch of extra code, you can add a className to the block directly in the template code. This is the same code that the OP used, with one small adjustment. "class" becomes "className":

function myplugin_register_book_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Books',
        'show_in_rest' => true,
        'template' => array(
            array( 'core/image', array(
                'align' => 'left',
            ) ),
            array( 'core/heading', array(
                'placeholder' => 'Add Author...',
                'className' => 'blockHeadStyle anotherClassName'
            ) ),
            array( 'core/paragraph', array(
                'placeholder' => 'Add Description...',
            )),
        ),
    );
    register_post_type('book', $args);
}
add_action('init', 'myplugin_register_book_post_type');

Here is a screenshot to show how this renders in dev tools:

As you can see, I also added a second class to the h2 element, simply by adding a space between each class declared by "className."

This can be done using the block filters API.

Here is an example of adding a class to the block in the editor.


const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
    return ( props ) => {
        const customClass = 'custom-classname' );
        return (
            <BlockListBlock { ...props } className={ customClass }/>
        );
    };
}, 'withClientIdClassName' );

addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName );

The above code will add the custom-classname class to each block in the list of block in the editor.

If you're looking to only affect a specific block, props has name property that can be checked:


const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
    return ( props ) => {
        if( 'core/heading' === props.name ) {
            const customClass = 'custom-classname' );
            return (
                <BlockListBlock { ...props } className={ customClass }/>
            );
        } else {
             <BlockListBlock { ...props }/> 
        }
    };
}, 'withClientIdClassName' );

addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName );

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论