Custom Blocks With ACF 6

The creators of the Advanced Custom Fields plugin for WordPress, have recently launched a new update, version 6. The user interface has been greatly improved and is now more visually pleasing and easier to work with.

Does ACF 6 Works With My Old Code?

The answer is yes! Your old custom fields will still work as expected and you don’t have anything to alter. But, the ACF team recommends making one change and it’s the way your blocks are being registered. ACF 6 can now use the native WordPress function register_block_type() instead of acf_register_block_type(). This update will allow your blocks to take full advantage of any new upcoming features from WordPress and ACF.

New Way To Register A Block With ACF 6

One of the things that have changed is how a new custom block is registered and here is how to upgrade your code:

functions.php
add_action( 'init', 'register_acf_blocks', 5 );
function register_acf_blocks() {
    /* Register all your blocks here */
    register_block_type( get_template_directory() . '/blocks/testimonials' );
    // register_block_type( get_template_directory() . '/blocks/another-block' );
}

NOTE: Don’t forget that this will replaces your current acf_register_block_type() call for the block you’re upgrading.

JSON File Block Configurations

All the configurations for your block have now been moved into a JSON file as block.json.

/blocks/testimonials/block.json
{
    "name": "acf/testimonials",
    "title": "Testimonials",
    "description": "A custom Testimonials block.",
    "category": "formatting",
    "icon": "admin-comments",
    "keywords": ["testimonials", "quote"],
    "acf": {
        "mode": "preview",
        "renderCallback": "acf_block_render_testimonials"
    },
    "align": "full"
}

For more configurations, visit the official ACF website where you’ll find more details about what’s supported.

If you are creating a new block and need to know what’s next then just follow the steps 2 to 5 from my previous post WordPress Custom Blocks With ACF and you are done.

Leave a Reply

Your email address will not be published. Required fields are marked *