Registering field for acf block does not work

I created a block with acf_register_block_type which shows the block in editor which is complete fine. I did that this way,

//custom block with acf
function mab_register_acf_block_types() { acf_register_block_type( [ 'name' => 'blockquote', 'title' => __( 'Blockquote' ), 'description' => __( 'My blockquote block.' ), 'render_template' => dirname( __file__ ) . '/blocks/blockquote/blockquote.php', 'category' => 'formatting', 'icon' => 'format-quote', ] );
}
if ( function_exists( 'acf_register_block_type' ) ) { add_action( 'acf/init', 'mab_register_acf_block_types' );
}

But I wanted to register the field for this block which actually does not work. The registration of field done this way

add_action('acf/init', function () { if( function_exists('acf_add_local_field_group') ): acf_add_local_field_group(array ( 'key' => 'group_1', 'title' => 'My Group', 'fields' => array ( array ( 'key' => 'field_1', 'label' => 'Sub Title', 'name' => 'sub_title', 'type' => 'text', 'prefix' => '', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array ( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', 'readonly' => 0, 'disabled' => 0, ) ), 'location' => array ( array ( array ( 'param' => 'block', 'operator' => '==', 'value' => 'blockquote', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', )); endif; });

If I set the location to something else like post_type , it is okay, which means the code for registering the field is working for post_type but if I set the location to block == 'blockquote', it's not working. Is there anything wrong I am doing or is it the bug of acf ?

1 Answer

I'm using ACF (Pro) 6.1.7 but when registering the fields; the callback function is hooked into 'acf/include_fields' not 'acf/init'.

Secondly, and very likely the cause of your issue: Even though don't specify a namespace in acf_register_block_type(name) (and you should not specify a namespace in acf_register_block_type); ['location'][0][0]['value'] in your callback function, acf_add_local_field_group, should be 'acf/blockquote' not just 'blockquote'.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like