How to find Reusable Blocks and add them in the Admin Menu

Reusable blocks utilizes the custom post type data saving methodology embedded in WordPress. it joins the many other in-built custom post types like the navigation, media, posts and pages.

However, you are not able to see this post type in the admin menu because it is hidden by default in WordPress core. However, like all post types, you can access them via the url with there post type name/id.

To quickly access Reusable Blocks, use the link below:

https://example.com/wp-admin/edit.php?post_type=wp_block

However, you need to change the domain (it is omukiguy.com in the code above) to your particular setup.

Screenshot showing the resuable post type in the admin panel

However, you might require this to be visible all the time so, adding this code sample below to your functions.php or as a separate plugin would be helpful.

/**
 * Add Reusable Blocks Menu Item to Admin Menu.
 * 
 * return void
 */
function be_reusable_blocks_admin_menu() {
    add_menu_page(
        'Reusable Blocks',
        'Reusable Blocks',
        'edit_posts',
        'edit.php?post_type=wp_block',
        '',
        'dashicons-editor-table',
        22,
    );
}

add_action( 'admin_menu', 'be_reusable_blocks_admin_menu' );

You will get a menu item as in the screenshot below. This allows you to delete/edit/create new reusable blocks or even import from another site to yours.

Reusable Blocks Menu Item in the Admin Menu

Disable creation and use of reusable blocks

/**
 * @see: https://github.com/WordPress/gutenberg/issues/28895#issuecomment-777444618
 */
import { addFilter } from '@wordpress/hooks';
import { select, dispatch, subscribe } from '@wordpress/data';

// Erase the existence of any reusable blocks.
subscribe( () => {
	const settings = select( 'core/block-editor' ).getSettings();
	if ( settings.__experimentalReusableBlocks && settings.__experimentalReusableBlocks.length > 0 ) {
		dispatch('core/block-editor').updateSettings( { __experimentalReusableBlocks: [] } );
	}
});

/**
 * Remove supported features from all blocks
 *
 * @param {*} settings
 * @param {*} name
 */
function filterBlockSupports( settings, name ) {
	// ensure there is a supports section
	if ( undefined === settings.supports ) {
		settings.supports = {}
	}
	settings.supports.reusable = false;

	return settings;
}
addFilter( 'blocks.registerBlockType', 'tomjn/disable-reusability', filterBlockSupports );


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *