|
16 | 16 | /** |
17 | 17 | * Enqueue Gutenberg block assets for both frontend + backend. |
18 | 18 | * |
| 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. |
19 | 27 | * @uses {wp-editor} for WP editor styles. |
20 | 28 | * @since 1.0.0 |
21 | 29 | */ |
22 | 30 | 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( |
25 | 35 | '<% blockNamePHPLower %>-cgb-style-css', // Handle. |
26 | 36 | 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. |
29 | 39 | ); |
30 | | -} |
31 | | - |
32 | | -// Hook: Frontend assets. |
33 | | -add_action( 'enqueue_block_assets', '<% blockNamePHPLower %>_cgb_block_assets' ); |
34 | 40 |
|
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( |
47 | 45 | '<% blockNamePHPLower %>-cgb-block-js', // Handle. |
48 | 46 | plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack. |
49 | 47 | 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. |
51 | 49 | true // Enqueue the script in the footer. |
52 | 50 | ); |
53 | 51 |
|
54 | | - // Styles. |
55 | | - wp_enqueue_style( |
| 52 | + /** |
| 53 | + * Register block editor styles for backend. |
| 54 | + */ |
| 55 | + wp_register_style( |
56 | 56 | '<% blockNamePHPLower %>-cgb-block-editor-css', // Handle. |
57 | 57 | 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. |
60 | 60 | ); |
| 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 | + ) ); |
61 | 76 | } |
62 | 77 |
|
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