Skip to content

Commit 0f1ad17

Browse files
authored
📦 NEW: Add init hook to enqueue block assets
This commit fixes issue #21 - `init` hook added to enqueue block assets on the server-side using `register_block_type` function. - Adds `null` as the version parameter for block assets.
1 parent 4dcaa0c commit 0f1ad17

1 file changed

Lines changed: 42 additions & 27 deletions

File tree

  • packages/cgb-scripts/template/src

‎packages/cgb-scripts/template/src/init.php‎

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,64 @@
1616
/**
1717
* Enqueue Gutenberg block assets for both frontend + backend.
1818
*
19+
* Assets enqueued:
20+
* 1. blocks.style.build.css - Frontend + Backend.
21+
* 2. blocks.build.js - Backend.
22+
* 3. blocks.editor.build.css - Backend.
23+
*
24+
* @uses {wp-blocks} for block type registration & related functions.
25+
* @uses {wp-element} for WP Element abstraction — structure of blocks.
26+
* @uses {wp-i18n} to internationalize the block's text.
1927
* @uses {wp-editor} for WP editor styles.
2028
* @since 1.0.0
2129
*/
2230
function <% blockNamePHPLower %>_cgb_block_assets() { // phpcs:ignore
23-
// Styles.
24-
wp_enqueue_style(
31+
/**
32+
* Register block styles for both frontend + backend.
33+
*/
34+
wp_register_style(
2535
'<% blockNamePHPLower %>-cgb-style-css', // Handle.
2636
plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ), // Block style CSS.
27-
array( 'wp-editor' ) // Dependency to include the CSS after it.
28-
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
37+
array( 'wp-editor' ), // Dependency to include the CSS after it.
38+
null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
2939
);
30-
}
31-
32-
// Hook: Frontend assets.
33-
add_action( 'enqueue_block_assets', '<% blockNamePHPLower %>_cgb_block_assets' );
3440

35-
/**
36-
* Enqueue Gutenberg block assets for backend editor.
37-
*
38-
* @uses {wp-blocks} for block type registration & related functions.
39-
* @uses {wp-element} for WP Element abstraction — structure of blocks.
40-
* @uses {wp-i18n} to internationalize the block's text.
41-
* @uses {wp-editor} for WP editor styles.
42-
* @since 1.0.0
43-
*/
44-
function <% blockNamePHPLower %>_cgb_editor_assets() { // phpcs:ignore
45-
// Scripts.
46-
wp_enqueue_script(
41+
/**
42+
* Register block editor script for backend.
43+
*/
44+
wp_register_script(
4745
'<% blockNamePHPLower %>-cgb-block-js', // Handle.
4846
plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack.
4947
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above.
50-
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: File modification time.
48+
null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time.
5149
true // Enqueue the script in the footer.
5250
);
5351

54-
// Styles.
55-
wp_enqueue_style(
52+
/**
53+
* Register block editor styles for backend.
54+
*/
55+
wp_register_style(
5656
'<% blockNamePHPLower %>-cgb-block-editor-css', // Handle.
5757
plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS.
58-
array( 'wp-edit-blocks' ) // Dependency to include the CSS after it.
59-
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
58+
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
59+
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
6060
);
61+
62+
/**
63+
* Register Gutenberg block on server-side.
64+
*
65+
* Register the block on server-side to ensure that the block
66+
* scripts and styles for both frontend and backend are
67+
* enqueued when the editor loads.
68+
*
69+
* @link https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type#enqueuing-block-scripts
70+
*/
71+
register_block_type( 'cgb/block-<% blockName %>', array(
72+
'style' => '<% blockNamePHPLower %>-cgb-style-css', // Enqueue blocks.style.build.css on both frontend & backend.
73+
'editor_script' => '<% blockNamePHPLower %>-cgb-block-js', // Enqueue blocks.build.js in the editor only.
74+
'editor_style' => '<% blockNamePHPLower %>-cgb-block-editor-css', // Enqueue blocks.editor.build.css in the editor only.
75+
) );
6176
}
6277

63-
// Hook: Editor assets.
64-
add_action( 'enqueue_block_editor_assets', '<% blockNamePHPLower %>_cgb_editor_assets' );
78+
// Hook: Block assets.
79+
add_action( 'init', '<% blockNamePHPLower %>_cgb_block_assets' );

0 commit comments

Comments
 (0)