Custom WordPress block doesn't appear in the block search of the post editor

73 Views Asked by At

I am developing a custom WordPress Block as a plugin for my WordPress project. But I am new to developing custom block.

This is the code of the plugin.

<?php
/*
Plugin Name: Custom CS Banner
Version: 1.0
*/

function custom_cs_banner_render( $attributes ) {
    $title = $attributes['title'];

    return '<div class="custom-cs-banner-block">' .
        '<h2>' . $title . '</h2>' .
        '</div>';
}

function custom_cs_banner_init() {
    wp_register_script(
        'custom-cs-banner-script',
        plugins_url( 'custom-cs-banner.js', __FILE__ ),
        ['wp-blocks', 'wp-element' ]
    );

    wp_register_style(
        'custom-cs-banner-style',
        plugins_url( 'custom-cs-banner.css', __FILE__ ),
        [ 'wp-edit-blocks' ],
    );

    add_action('custom_cs_banner_render', 'custom_cs_banner_render');

    register_block_type( 'customblocks/customcsbanner', [
        'api_version' => 1,
        'title' => 'Custom CS Banner',
        'category' => 'widgets',
        'attributes' => [
            'title' => [
                'type' => 'string',
                'default' => '',
                'source' => 'html',
                'selector' => 'h2',
            ],
        ],
        'editor_script' => 'custom-cs-banner-script',
        'editor_style' => 'custom-cs-banner-style',
        'render_callback' => 'custom_cs_banner_render',
    ] );
}

add_action( 'init', 'custom_cs_banner_init' );

I actived the plugin and checked that the block is registered successfully using below code.

$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
print_r($block_types);

There was the block "customblocks/customcsbanner" at the print result. But the block didn't appear at the search result for "customcsbanner" of the post editor. I would like you to give me an advice if you know the reason. Thanks.

0

There are 0 best solutions below