Halloween Sale 15% off Ends in See the plans
PHP

Your Own Block Category at the Top of the Inserter

Adds a block category named after your site to the top of the block inserter and moves custom blocks with a chosen namespace into it, so editors find your bespoke blocks first.

Don’t have WPIDE yet? Install it on your site

Code

// Block name prefixes that belong in the category.
$wpide_block_namespaces = array( 'wpide/' );

add_filter( 'block_categories_all', function ( $categories ) {
    array_unshift( $categories, array(
        'slug'  => 'site-blocks',
        'title' => get_bloginfo( 'name' ),
        'icon'  => null,
    ) );
    return $categories;
} );

add_filter( 'register_block_type_args', function ( $args, $name ) use ( $wpide_block_namespaces ) {
    foreach ( $wpide_block_namespaces as $prefix ) {
        if ( 0 === strpos( $name, $prefix ) ) {
            $args['category'] = 'site-blocks';
        }
    }
    return $args;
}, 10, 2 );

Notes

Set $wpide_block_namespaces to the prefix of your blocks, such as acme/ or wpide/.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.