Support

Account

Home Forums Gutenberg ACF Blocks – Need stable id Reply To: ACF Blocks – Need stable id

  • I can into the same problem with the suggested solution from ACF. But further research showed that only attributes set directly in the $block scope are reset. Setting it in $block[‘data’] makes it persistent

    function mypre_add_anchor_to_blocks( $attributes ) {
    
    	error_log('attributes');
    	error_log( print_r( $attributes, true ) );
    
        if( !isset($attributes['data']['customvalue']) || empty($attributes['data']['anchor']) ) {
            $attributes['data']['customvalue'] = 'block-' . uniqid();
        }
        return $attributes;
    }
    add_filter('acf/pre_save_block', 'mypre_add_anchor_to_blocks');

    Notice – on first save of the page you need to reload the admin page with the editor to make the block include the new attribute.