wp-gutenberg-block-template/index.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2020-08-12 13:20:11 +00:00
<?php
2020-08-12 19:27:36 +00:00
define( 'BLOCK_NAMESPACE', '<<INSERT BLOCK NAMESPACE HERE>>' );
2020-08-12 19:21:01 +00:00
$block_namespace = BLOCK_NAMESPACE;
2020-08-12 18:50:04 +00:00
$block_slug = basename( dirname( __FILE__ ) );
2020-08-12 18:29:39 +00:00
add_action( 'init', function() {
2020-08-12 18:52:02 +00:00
global $block_slug, $block_namespace;
2020-08-12 18:29:39 +00:00
2020-08-12 13:20:11 +00:00
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
2020-08-12 18:29:39 +00:00
$dir = get_template_directory() . "/blocks/$block_slug";
$uri = get_template_directory_uri() . "/blocks/$block_slug";
$asset_file = include( get_template_directory() . "/blocks/$block_slug/build/index.asset.php");
2020-08-12 13:20:11 +00:00
$index_js = 'build/index.js';
wp_register_script(
2020-08-12 18:29:39 +00:00
"{$block_slug}-block-editor",
2020-08-12 13:20:11 +00:00
"$uri/$index_js",
$asset_file['dependencies'],
$asset_file['version']
);
2020-08-12 13:55:40 +00:00
$editor_css = 'build/index.css';
2020-08-12 13:20:11 +00:00
wp_register_style(
2020-08-12 18:29:39 +00:00
"{$block_slug}-block-editor",
2020-08-12 13:20:11 +00:00
"$uri/$editor_css",
array(),
filemtime( "$dir/$editor_css" )
);
2020-08-12 13:55:40 +00:00
$style_css = 'build/style-index.css';
2020-08-12 13:20:11 +00:00
wp_register_style(
2020-08-12 18:29:39 +00:00
"{$block_slug}-block",
2020-08-12 13:20:11 +00:00
"$uri/$style_css",
array(),
filemtime( "$dir/$style_css" )
);
2020-08-12 18:52:02 +00:00
register_block_type( "$block_namespace/$block_slug", array(
2020-08-12 18:29:39 +00:00
'editor_script' => "{$block_slug}-block-editor",
'editor_style' => "{$block_slug}-block-editor",
'style' => "{$block_slug}-block",
2020-08-12 13:20:11 +00:00
) );
2020-08-12 18:29:39 +00:00
} );