I'm trying to include a frontend script for my block plugin that is registered via block.json
. Below is what I have:
/
plugin.php
block.json
scorecard.php
scorecard.css
scorecard.js
plugin.php
add_action( 'init', 'sf_scorecard_block_init' );
function sf_scorecard_block_init() {
register_block_type_from_metadata( __DIR__ );
}
block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"name": "sf/scorecard",
"title": "Deal Scorecard",
"description": "Display a scorecard for the selected Deal.",
"viewScript": [ "file:./scorecard.js" ],
"style": [ "file:./scorecard.css" ],
"category": "embed",
"textdomain": "sf",
"icon": "yes-alt",
"keywords": [ "scorecard", "deal" ],
"acf": {
"mode": "auto",
"renderTemplate": "scorecard.php"
},
"align": "full"
}
scorecard.js
console.log('loaded');
Everything with the block is working as expected (including the CSS file), but I cannot get the scorecard.js
file to load and fire. I'm on WordPress 6.1, so viewScript
should work according to the docs.
I'm also using this block in conjunction with ACF, hence the acf.renderTemplate
key. Not sure if that interferes with this or not...
Surely I'm missing something, but the documentation seems confusing on this topic. Most examples I see register the block via PHP and not block.json
.
acf
portion and used the standardrender
key. The block still pulls in, but the JS does not. No console or network errors.