Advertisement
michaellevelup

Disallow all and allow only specific blocks

Sep 18th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. function king_disallow_block_types( $disallowed_block_types, $block_editor_context ) {
  2.  
  3.     $allowed_blocks = array(
  4.         'core/heading',
  5.         'core/paragraph',
  6.         'kadence/accordion'
  7.     );
  8.    
  9.    
  10.     if ( ! is_array( $disallowed_block_types ) || empty( $disallowed_block_types ) ) {
  11.         $registered_blocks   = WP_Block_Type_Registry::get_instance()->get_all_registered();
  12.         $disallowed_block_types = array_keys( $registered_blocks );
  13.     }
  14.  
  15.     // Create a new array for the allowed blocks.
  16.     $filtered_blocks = array();
  17.  
  18.     foreach ( $disallowed_block_types as $block ) {
  19.  
  20.         if ( in_array( $block, $allowed_blocks, true ) ) {
  21.  
  22.             // If it's not disallowed, add it to the filtered list.
  23.             $filtered_blocks[] = $block;
  24.         }
  25.     }
  26.  
  27.     // Return the filtered list of allowed blocks
  28.     return $filtered_blocks;
  29. }
  30.  
  31. add_filter( 'allowed_block_types_all', 'king_disallow_block_types', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement