Last active
November 13, 2018 18:58
-
-
Save eudesgit/25ab0ebda4dd761e447345768ade462b to your computer and use it in GitHub Desktop.
Registers the scripts and styles to create a WordPress Gutenberg block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Registers a Gutenberg block JS script and its styles | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
public function register_block_action ( ) { | |
$block_name = 'my_block'; | |
$block_namespace = 'gutenberg-blocks-sample/' . $block_name; | |
$script_slug = $this->plugin_name . '-' . $block_name; | |
$style_slug = $this->plugin_name . '-' . $block_name . '-style'; | |
$editor_style_slug = $this->plugin_name . '-' . $block_name . '-editor-style'; | |
// The JS block script | |
$script_file = $block_name . '/block.build.js'; | |
wp_enqueue_script( | |
$script_slug, | |
plugin_dir_url(__FILE__) . $script_file, | |
[ // Dependencies that will have to be imported on the block JS file | |
'wp-blocks', // Required: contains registerBlockType function that creates a block | |
'wp-element', // Required: contains element function that handles HTML markup | |
'wp-editor', // Required: contains RichText component for editable inputs | |
'wp-i18n', // contains registerBlockType function that creates a block | |
], | |
filemtime(plugin_dir_path(__FILE__) . $script_file) | |
); | |
// The block style | |
// It will be loaded on the editor and on the site | |
wp_register_style( | |
$style_slug, | |
plugin_dir_url( __FILE__ ) . $block_name . '/css/style.css', | |
['wp-blocks'], // General style | |
GBC_VERSION | |
); | |
// The block style for the editor only | |
wp_register_style( | |
$editor_style_slug, | |
plugin_dir_url( __FILE__ ) . $block_name . '/css/editor.css', | |
['wp-edit-blocks'], // Style for the editor | |
GBC_VERSION | |
); | |
// Registering the block | |
register_block_type( | |
$block_namespace, // Block name with namespace | |
[ | |
'style' => $style_slug, // General block style slug | |
'editor_style' => $editor_style_slug, // Editor block style slug | |
'editor_script' => $script_slug, // The block script slug | |
] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment