If I have declared, viewScript
in my block.json file. Do I need to enqueue the script manually within my register_block_type();
function also? (I didn't think it was necessary for 5.9?)
My Block.json
"textdomain": "postcode-pricing-wizard",
"editorScript": "file:./index.js",
"viewScript": "file:./view.js",
"style": "file:./style-index.css"
My Problem
I've enqueued the script, as shown above, I can see a completed build directory and I can also see my block within the editor.
view.js
However, isn't loading for me on my front-end? I'm not too sure why?
Unless I've misinterpreted the doc's Block Editor Handbook - Metadata
{ "viewScript": "file:./build/view.js" }
// Block type frontend script definition.
// It will be enqueued only when viewing the content on the front of the site.
// Since WordPress 5.9.0 (My WP Version - 5.9)
--
Here's my register_block_type()
function just in case it's needed.
register_block_type( PPW_DIR_PATH . '/build/pricing-wizard-block/',
array(
'render_callback' => function( $attributes, $content ) {
if(!is_admin()) {
wp_localize_script( 'wp-block-ppw-postcode-pricing-wizard-js', 'data', ['ajax_url' => admin_url('admin-ajax.php')]);
}
ob_start();
include_once plugin_dir_path( __FILE__ ) . '/includes/block-editor/pricing-wizard-block/views' . '/block.php';
return ob_get_clean();
},
)
);
admin-ajax.php
files location, is there a reason you chose to use the legacy AJAX API instead of the modern REST API for AJAX actions? Are you using v5.9?