For some reason, this is what I got working, at least for now:
block.json
{
"name": "acf/clb-custom-info-card",
"title": "Info Card",
"description": "A custom info card block.",
"style": [ "file:./clb_custom_info_card.css" ],
"category": "common",
"icon": "admin-comments",
"keywords": ["testimonial", "quote"],
"acf": {
"mode": "preview",
"renderTemplate": "clb_custom_info_card.php"
},
"align": "full",
"providesContext": {"acf/fields": "data"},
"attributes": {
"clb_custom_id": {
"type": "string",
"default": "1234abcd"
}
}
}
And then my acf/pre_save_block
filter:
add_filter(
'acf/pre_save_block',
function( $attributes ) {
//error_log('attributes');
error_log( print_r( $attributes, true ) );
// if ( empty( $attributes['clb_custom_id'] ) ) {
// $attributes['clb_custom_id'] = 'clb_custom_id-' . uniqid();
// }
if( !$attributes['clb_custom_id'] ) { $attributes['clb_custom_id'] = uniqid(); }
// if ( empty( $attributes['data']['clb_custom_id'] ) ) {
// $attributes['data']['clb_custom_id'] = 'clb_custom_id-' . uniqid();
// }
return $attributes;
}
);
Sure, here’s my block.json
file:
{
"name": "acf/clb-custom-info-card",
"title": "Info Card",
"description": "A custom info card block.",
"style": [ "file:./clb_custom_info_card.css" ],
"category": "common",
"icon": "admin-comments",
"keywords": ["testimonial", "quote"],
"acf": {
"mode": "preview",
"renderTemplate": "clb_custom_info_card.php"
},
"align": "full",
}
OK, we are getting close. I am new to using block.json
, but I am starting to use it now with ACF Blocks 2.0. Could you show me how you registered and declared that data? Thank you!
Thank you! I was hoping that ACF would have more documentation around this, but I understand that version 2.0 of ACF Blocks are very new. Please update me when you have more info.
Still unfortunately not working. Here is my filter code:
add_filter(
'acf/pre_save_block',
function( $attributes ) {
error_log( print_r( $attributes, true ) );
if ( empty( $attributes['data']['clb-custom-anchor'] ) ) {
$attributes['data']['clb-custom-anchor'] = 'acf-block-' . uniqid();
}
return $attributes;
}
);
Here is a very short video to explain what is happening:
https://www.dropbox.com/s/locxy298szdvik0/Blocky%20-%20ACF%202.0%20Blocks%20Not%20Working.mov?dl=0
I really appreciate your help here! Thank you!
This seems promising, however in my testing the $attributes[‘data’][‘customvalue’] value is not saving correctly to the block. Every time I change the block, it is resetting the ‘customvalue’. That is, ACF is not seeing that it was already set in the block [‘data’]. Am I missing something? Thanks!
This solution (using the admin_enqueue_scripts
hook) worked for me – thank you!
+1 Agree – this would be extremely helpful
I was having the same problem. Here’s the code snippet that worked for me to solve it:
add_action( 'wp_head', 'clb_add_acf_form_functionality', 2);
function clb_add_acf_form_functionality() {
acf_form_head();
}
I am running ACF Pro 5.7.3 and getting the same problem (JS Type Error when trying to select from a dropdown list). I tried removing and then re-uploading the plugin. Are there any other fixes?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.