Skip to content

Commit 2b46abb

Browse files
authored
Merge pull request #3 from wptrainingteam/part-3-enqueue-additional-assets
add: enqueue additional assets
2 parents 5a69819 + 19572a7 commit 2b46abb

File tree

8 files changed

+65
-1
lines changed

8 files changed

+65
-1
lines changed

advanced-multi-block.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,36 @@ function register_blocks() {
4646
return;
4747
}
4848
}
49-
add_action( 'init', 'register_blocks' );
49+
add_action( 'init', 'register_blocks' );
50+
51+
/**
52+
* Enqueues the block assets for the editor
53+
*/
54+
function enqueue_block_assets() {
55+
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/editor-script.asset.php';
56+
57+
wp_enqueue_script(
58+
'editor-script-js',
59+
plugin_dir_url( __FILE__ ) . 'build/editor-script.js',
60+
$asset_file['dependencies'],
61+
$asset_file['version'],
62+
false
63+
);
64+
}
65+
add_action( 'enqueue_block_editor_assets', 'enqueue_block_assets' );
66+
67+
/**
68+
* Enqueues the block assets for the frontend
69+
*/
70+
function enqueue_frontend_assets() {
71+
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/frontend-script.asset.php';
72+
73+
wp_enqueue_script(
74+
'frontend-script-js',
75+
plugin_dir_url( __FILE__ ) . 'build/frontend-script.js',
76+
$asset_file['dependencies'],
77+
$asset_file['version'],
78+
true
79+
);
80+
}
81+
add_action( 'wp_enqueue_scripts', 'enqueue_frontend_assets' );

build/editor-script.asset.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array(), 'version' => '31d6cfe0d16ae931b73c');

build/editor-script.js

Whitespace-only changes.

build/frontend-script.asset.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array(), 'version' => '31d6cfe0d16ae931b73c');

build/frontend-script.js

Whitespace-only changes.

src/editor-script.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Block Editor Script Functionality
3+
*
4+
* The following scripts are compiled into a single asset and loaded into the block editor.
5+
*
6+
*/
7+
8+
// import editor scripts here.

src/frontend-script.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Frontend Script Functionality
3+
*
4+
* The following scripts are compiled into a single asset and loaded into the frontend.
5+
*
6+
*/
7+
8+
// import frontend scripts here.

webpack.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const [ scriptConfig, moduleConfig, ] = require('@wordpress/scripts/config/webpack.config');
2+
const path = require('path');
3+
4+
module.exports = [
5+
{
6+
...scriptConfig,
7+
entry: {
8+
...scriptConfig.entry(),
9+
'editor-script': path.resolve(__dirname, 'src/editor-script.js'),
10+
'frontend-script': path.resolve(__dirname, 'src/frontend-script.js'),
11+
},
12+
},
13+
moduleConfig,
14+
];

0 commit comments

Comments
 (0)