advanced custom fields – $attributes not defined in block.json PHP template for ACF blocks
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!
Leave an answer