0

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.

2
  • the use of ACF very likely interferes with it, have you confirmed this with a simple block that doesn't use ACF? And have you confirmed there are no PHP warnings or notices or errors in the JS console/network tab?
    – Tom J Nowell
    Commented Nov 6, 2022 at 22:08
  • Yes, I have tried removing the acf portion and used the standard render key. The block still pulls in, but the JS does not. No console or network errors.
    – scferg5
    Commented Nov 6, 2022 at 22:14

0

Your Answer

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

Browse other questions tagged or ask your own question.