1

I'm using ACF blocks to create custom dynamic blocks. Here is the code in the block.json file -

{
    "title": "Full Banner",
    "name": "lionsroar/full-banner",
    "category": "content",
    "icon": "slides",
    "viewScript": "full-banner-js",
    "style": "full-banner-css",
    "attributes": {
        "title": {
            "type": "string",
            "default": ""
        }
    },
    "variations": [
        {
            "name": "header-podcast",
            "title": "Header - Podcast",
            "description": "Header for the Podcasts page",            
            "attributes": {
                "title": "podcast"
            },
            "isDefault": true
        },
        {
            "name": "header-audio",
            "title": "Header - Audio",
            "description": "Header for the Audio page",
            "attributes": {
                "title": "audio"
            }
        },
        {
            "name": "header-meditation",
            "title": "Header - Medidation",
            "description": "Header for the Medidation page",
            "attributes": {
                "title": "med"
            }
        },
        {
            "name": "header-meditation-alt",
            "title": "Header - Meditation (Alt)",
            "description": "Alternate Header for the Podcasts page",
            "attributes": {
                "title": "med-alt"
            }
        }
    ],
    "acf": {
      "mode": "preview",
      "renderTemplate": "full-banner.php"
    }
}

As you can see, I'm trying to create a block with variations and add different attribute for every variation.

The problem I'm having is I'm not able to access the attributes in the full-banner.php PHP file. While the official docs say that attributes are available in a $attributes variable. But it is showing undefined at my end. I even tried removing the variants. Even then I cannot access the attributes. Nothing worked.

As per this thread, it should work but somehow it's not!

Any kind of help in the right direction is appreciated!

2
  • The official docs are referring to the template set using render property. You're using ACF with the acf.renderTemplate property, so you'll need to check ACF's documentation for how to use that template. May 31, 2023 at 7:43
  • Yea, it was the case. The attributes are part of the $block variable available in PHP file. Thanks for the help! Jun 1, 2023 at 16:13

1 Answer 1

1

It turns out ACF blocks provide the attributes in the $block variable available in the PHP file mentioned in renderTemplate property of block.json. So, in my case it was available as $block['title'].

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.