Skip to content

Commit 7626acb

Browse files
ahmadbilaldevahmadawais
authored andcommitted
πŸ“¦ NEW: The wp_localize_script functionality (#206)
* πŸ“¦ NEW: wp_localize_script in template * πŸ“¦ NEW: wp_localize_script in template * πŸ“¦ NEW: Update globals in webpack configuration * πŸ‘Œ IMPROVE: Spacing * πŸ‘Œ IMPROVE: Global names * πŸ‘Œ IMPROVE: wp_localize_script * πŸ‘Œ IMPROVE: Spacing Co-Authored-By: Ahmad Awais ⚑️ <me@AhmadAwais.com>
1 parent c7cbd7a commit 7626acb

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

β€Žpackages/cgb-scripts/config/externals.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const externals = [
4242
jquery: 'jQuery', // import $ from 'jquery' // Use the WordPress version after enqueuing it.
4343
'react-dom': 'ReactDOM',
4444
lodash: 'lodash', // Lodash is there in Gutenberg.
45+
CGB_GLOBAL: 'CGB_GLOBAL' // Add custom dynamic data to a global var.
4546
}
4647
);
4748

β€Žpackages/cgb-scripts/template/src/init.phpβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ function <% blockNamePHPLower %>_cgb_block_assets() { // phpcs:ignore
5353
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
5454
);
5555

56+
// Localise globals - plugin path and url for use in blocks.
57+
wp_localize_script(
58+
'<% blockNamePHPLower %>-cgb-block-js',
59+
'CGB_GLOBAL', // Array containing dynamic data for a JS Global.
60+
[
61+
'path' => plugin_dir_path( __FILE__ ),
62+
'url' => plugin_dir_url( __FILE__ ),
63+
]
64+
);
65+
5666
/**
5767
* Register Gutenberg block on server-side.
5868
*

0 commit comments

Comments
Β (0)