Add Reusable blocks to WordPress admin menu

Reusable blocks are no-so-much-known function but very handy function of WordPress Gutenberg editor. With them you can easily define a block (or series of blocks) that will share the content, so that you can re-use them anywhere and they will be the same.

You can also edit them in one place and anytime you can convert them to standard block, if you want to edit just one occurrence of the block. There is even an extra feature – you can export your reusable block to JSON and also import them.

The only problem is, that they are hard to manage – you must go to a post/page, click + for adding a new block, then switch to Reusable tab and then click Manage all reusable blocks, that will transfer you to /edit.php?post_type=wp_block page.

BTW – if you are little more experienced WP developer you simply from that given URI understand, how Reusable blocks work – they are simple a custom post type, where title is the name of the block and content is the block itself, so you can work with that as with any other CPT.

If you want to give editors easy way to manage reusable blocks, you can add the link to the management page to the menu. E.g. this action will add it as a submenu to both Pages and Posts menu for everyone with edit posts capability. There is also a plugin Reusable blocks extended that add such menu plus some other features.

function kapler_admin_menu() {
    global $submenu;
    $permalink = admin_url( 'edit.php' ).'?post_type=wp_block';
    $submenu['edit.php'][] = array( __('Reusable Blocks'), 'edit_posts', $permalink );
    $submenu['edit.php?post_type=page'][] = array( __('Reusable Blocks'), 'edit_posts', $permalink );
add_action( 'admin_menu', 'kapler_admin_menu' );






Napsat komentář