Support

Account

Home Forums Backend Issues (wp-admin) Register allowed block types in both functions.php and a plugin

Unread

Register allowed block types in both functions.php and a plugin

  • Hello there.

    I’m currently developing a plugin for a single custom ACF block, that will be added to the allowed block types of the core.

    My problem is, that when declaring a new allowed block type by calling 'allowed_block_types' inside the plugin, it will override the ones already declared within the themes functions.php.

    Is there a way that I can declare new allowed block types from within the plugin, without overriding existing blocks?

    functions.php:

    
    // CUSTOM ACF BLOCKS
    add_action('acf/init', 'my_acf_init');
    function my_acf_init() {
    
      if( function_exists('acf_register_block') ) {
    
    	// Register Block
    	acf_register_block(array(
    		'name' => 'front-page-header',
    		'title' => __('Header'),
    		'description' => __('Header shown at the top of the front page.'),
    		'render_callback'	=> 'acf_callback',
    		'category' => 'formatting',
    		'supports' => array( 'mode' => false, 'align' => false ),
    		'icon' => 'align-wide',
    		'mode' => 'edit',
    		'keywords' => array( 'header', 'heading' ),
    	));
    
       } 
    }
    
    // Disable Default Gutenberg Blocks DEFAULT GUTENBERG BLOCKS
    add_filter( 'allowed_block_types', 'acf_allowed_block_types' );
    function acf_allowed_block_types($array) {
    	$stack = array(
    
    	// CUSTOM
    	'acf/front-page-header',
    	
    	);
    
    	return $stack;
    }
    

    plugin.php:

    
    // CUSTOM ACF BLOCKS
    add_action('acf/init', 'my_acf_init1');
    function my_acf_init1() {
    
      if( function_exists('acf_register_block') ) {
    
    	// Register Block
    	acf_register_block(array(
    		'name' => 'acf-section-employees',
    		'title' => __('Section: Employees'),
    		'description' => __('Section that shows employees'),
    		'render_callback'	=> 'acf_callback',
    		'category' => 'formatting',
    		'supports' => array( 'mode' => false, 'align' => false ),
    		'icon' => 'align-wide',
    		'mode' => 'edit',
    		'keywords' => array( 'employees' ),
    	));
    
       }
    }
    
    // Disable Default Gutenberg Blocks DEFAULT GUTENBERG BLOCKS
    add_filter( 'allowed_block_types', 'acf_allowed_block_types_employee' );
    function acf_allowed_block_types_employee($array) {
    	$stack = array(
    
    	// CUSTOM
    	'acf/acf-section-employees',
    	
    	);
        return $stack;
    }
    

    The block declared inside the plugin will not be shown, because the filter in functions.php will override it. Is there a way around that? So both block types can be registered in different places (functions.php and plugin.php).

    Thank you so much in advance.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.