Support

Account

Home Forums Backend Issues (wp-admin) Problem with acf custom bock and filter allowed_block_types_all

Solved

Problem with acf custom bock and filter allowed_block_types_all

  • Hi all,
    I’ve 5 acf custom block.
    I’d like to manage “current” wordpress’s blocks displaying if specific plugin is active.

    For my first four acf custom block no problem i can manage.

    But for the last one i can’t manage it by this code :

    
    function my_allowed_block_types( $block_editor_context, $editor_context ) {
    
    	return array(
        'core/heading',
        'core/image',
    	'core/paragraph',
    	'core/spacer',
    	'core/gallery',
    	'core/list',
    	'core/audio',
    	'core/file',
    	'core/video',
    	'core/table',
    	'core/buttons',
    	'core/columns',
    	'core/media-text',
    	'core/separator',
    	'core/quote',
    	'core/html',
    	'core/embed',
    	'acf/diaporama',
    	'acf/prix-services',
    	'acf/block-line',
    	'acf/intro',
    	'acf/custom_btn',
    	);
    }
    
    if(!is_plugin_active('woo-gutenberg-products-block/woocommerce-gutenberg-products-block.php')){
    
    	add_filter( 'allowed_block_types_all', 'my_allowed_block_types',10,2);
    
    }
    

    The code for my custom block is :

    
     function register_blocks_btn() {
        
        if( ! function_exists( 'acf_register_block_type' ) )
          return;
    
        acf_register_block_type( array(
          'name'      => 'custom_btn',
          'title'     => __( 'Call To Action'),
          'render_template' => WP_CONTENT_DIR.'/mu-plugins/inc/custom-block/custom_btn/render_template/template_btn.php',
          'category'    => 'common',
          'icon'      => 'format-gallery',
          'mode'      => 'preview',
          'keywords'    => array( 'call to action', 'cta', 'bouton','lien'),
          'post_types' => array('post','page'),
        ));
    
      }
    add_action('acf/init', 'register_blocks_btn' );
    
    function field_btn(){
    
      acf_add_local_field_group(array(
        'key' => 'group_62664383cb2be',
        'title' => 'Positionnement',
        'fields' => array(
            array(
              'key' => 'field_62664389bf461',
              'label' => 'Lien de votre bouton',
              'name' => 'lien_de_votre_bouton',
              'type' => 'link',
              'instructions' => '',
              'required' => 1,
              'conditional_logic' => 0,
              'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
              ),
              'default_value' => '',
              'placeholder' => '',
            ),
          array(
            'key' => 'field_62664cf80b5c6',
            'label' => 'intitulé du bouton',
            'name' => 'btn_txt',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
              'width' => '',
              'class' => '',
              'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
          ),
        ),
        'location' => array(
          array(
            array(
              'param' => 'block',
              'operator' => '==',
              'value' => 'acf/custom-btn',
            ),
          ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
        'show_in_rest' => 0,
      ));
    
    }
    add_action('acf/init', 'field_btn');
    
      function register_blocks_btn() {
        
        if( ! function_exists( 'acf_register_block_type' ) )
          return;
    
        acf_register_block_type( array(
          'name'      => 'custom_btn',
          'title'     => __( 'Call To Action'),
          'render_template' => WP_CONTENT_DIR.'/mu-plugins/inc/custom-block/custom_btn/render_template/template_btn.php',
          'category'    => 'common',
          'icon'      => 'format-gallery',
          'mode'      => 'preview',
          'keywords'    => array( 'call to action', 'cta', 'bouton','lien'),
          'post_types' => array('post','page'),
        ));
    
      }
    add_action('acf/init', 'register_blocks_btn' );
    
    function field_btn(){
    
      acf_add_local_field_group(array(
        'key' => 'group_62664383cb2be',
        'title' => 'Positionnement',
        'fields' => array(
            array(
              'key' => 'field_62664389bf461',
              'label' => 'Lien de votre bouton',
              'name' => 'lien_de_votre_bouton',
              'type' => 'link',
              'instructions' => '',
              'required' => 1,
              'conditional_logic' => 0,
              'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
              ),
              'default_value' => '',
              'placeholder' => '',
            ),
          array(
            'key' => 'field_62664cf80b5c6',
            'label' => 'intitulé du bouton',
            'name' => 'btn_txt',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
              'width' => '',
              'class' => '',
              'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
          ),
        ),
        'location' => array(
          array(
            array(
              'param' => 'block',
              'operator' => '==',
              'value' => 'acf/custom-btn',
            ),
          ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
        'show_in_rest' => 0,
      ));
    
    }
    add_action('acf/init', 'field_btn');
    
  • I would use the action activated_plugin.
    Read more here: https://wordpress.stackexchange.com….

  • No sorry uxbox.
    After lost many hours of search the problem was the name in “acf_register_block_type” function

    I wrote :

    'name' => 'custom_btn'

    And i needed to write :

    'name' => 'custom-btn'

    It is write somewhere in documentation ?

  • With acf_register_block_type() – yes:

    name (String) A unique name that identifies the block (without namespace). For example ‘testimonial’. Note: A block name can only contain lowercase alphanumeric characters and dashes, and must begin with a letter.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.