Monday, November 7, 2022
HomeWordPress Developmentplugins - Block registration with viewScript

plugins – Block registration with viewScript


I am attempting to incorporate a frontend script for my block plugin that’s registered by way of block.json. Beneath is what I’ve:

/
  plugin.php
  block.json
  scorecard.php
  scorecard.css
  scorecard.js

plugin.php

add_action( 'init', 'sf_scorecard_block_init' );
operate sf_scorecard_block_init() {
    register_block_type_from_metadata( __DIR__ );
}

block.json

{
    "$schema": "https://schemas.wp.org/trunk/block.json",
    "identify": "sf/scorecard",
    "title": "Deal Scorecard",
    "description": "Show a scorecard for the chosen Deal.",
    "viewScript": [ "file:./scorecard.js" ],
    "fashion": [ "file:./scorecard.css" ],
    "class": "embed",
    "textdomain": "sf",
    "icon": "yes-alt",
    "key phrases": [ "scorecard", "deal" ],
    "acf": {
        "mode": "auto",
        "renderTemplate": "scorecard.php"
    },
    "align": "full"
}

scorecard.js

console.log('loaded');

All the pieces with the block is working as anticipated (together with the CSS file), however I can not get the scorecard.js file to load and fireplace. I am on WordPress 6.1, so viewScript ought to work in response to the docs.

I am additionally utilizing this block along side ACF, therefore the acf.renderTemplate key. Undecided if that interferes with this or not…

Certainly I am lacking one thing, however the documentation appears complicated on this subject. Most examples I see register the block by way of PHP and never block.json.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments