mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-28 11:22:21 +08:00
40 lines
1.5 KiB
Text
40 lines
1.5 KiB
Text
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 );
|