How to hide / disbale AIOSEO Gutenberg Blocks ?
-
Hey,
I want to hide these Blocks for EVERY user (not just my account / the account im logged in) to declutter the blocks section for the people working with gutenberg editor, as we will not use them in content building.
How is this possible ?
-
Hi @menikan,
Thanks for reaching out!
At the moment, there’s no built-in setting in AIOSEO to hide specific Gutenberg blocks like Breadcrumbs, FAQ, HTML Sitemap, or Table of Contents from the block inserter for all users. This is a very specific customization and not something most users request, which is why it requires a bit of custom code to implement.
The easiest and safest way to do it is through a JavaScript snippet using the WPCode plugin. Here’s what to do:
- Go to Code Snippets > Add Snippet in your WordPress dashboard.
- Choose “Add Your Custom Code (New Snippet)” and select JavaScript Snippet.
- Give it a name like “Hide AIOSEO Blocks”.
- Paste this code:
wp.domReady(() => {
const blocksToRemove = [
'aioseo/breadcrumbs',
'aioseo/html-sitemap',
'aioseo/faq',
'aioseo/table-of-contents'
];
blocksToRemove.forEach(block => {
if (wp.blocks.getBlockType(block)) {
wp.blocks.unregisterBlockType(block);
}
});
});- Set the following snippet settings:
- Insert Method: Auto Insert
- Location: Admin Header
- Save and Activate the snippet.
That’s it! These blocks will now be hidden for all users in the Gutenberg block editor.
If you’re not familiar with adding code to your site, I’d recommend having a developer assist you with this. Since it’s a fairly specific customization, it’s best done carefully.
Let me know if you need help with anything else!
Hey,
thanks for your answer! In the meantime is solved it by putting this code into the functions.php of the theme to hide the blocks for non-admins.
/**
* Filters the list of allowed block types based on user capabilities.
*
* This function checks if the current user has the 'edit_theme_options' capability.
* If the user does not have this capability, certain blocks are removed from the
* list of allowed block types in the Editor.
*
* @param array|bool $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
* @param object $block_editor_context The current block editor context.
*
* @return array The filtered list of allowed block types. If the current user does not have
* the 'edit_theme_options' capability, the list will exclude the disallowed blocks.
*/
function example_disallow_block_types( $allowed_block_types, $block_editor_context ) {
// If the current user doesn't have the correct permissions, disallow blocks.
if ( ! current_user_can( 'edit_theme_options' ) ) {
$disallowed_blocks = array(
'aioseo/breadcrumbs',
'aioseo/faq',
'aioseo/html-sitemap',
'aioseo/table-of-contents',
);
// Get all registered blocks if $allowed_block_types is not already set.
if ( ! is_array( $allowed_block_types ) || empty( $allowed_block_types ) ) {
$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
$allowed_block_types = array_keys( $registered_blocks );
}
// Create a new array for the allowed blocks.
$filtered_blocks = array();
// Loop through each block in the allowed blocks list.
foreach ( $allowed_block_types as $block ) {
// Check if the block is not in the disallowed blocks list.
if ( ! in_array( $block, $disallowed_blocks, true ) ) {
// If it's not disallowed, add it to the filtered list.
$filtered_blocks[] = $block;
}
}
// Return the filtered list of allowed blocks
return $filtered_blocks;
}
return $allowed_block_types;
}
add_filter( 'allowed_block_types_all', 'example_disallow_block_types', 10, 2 );
The topic ‘How to hide / disbale AIOSEO Gutenberg Blocks ?’ is closed to new replies.