-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadvanced-multi-block.php
More file actions
81 lines (72 loc) · 2.51 KB
/
advanced-multi-block.php
File metadata and controls
81 lines (72 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* Plugin Name: Advanced Multi Block
* Description: Example block scaffolded with Create Block tool.
* Version: 0.1.0
* Requires at least: 6.7
* Requires PHP: 7.4
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: advanced-multi-block
*
* @package CreateBlock
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function register_blocks() {
$build_dir = __DIR__ . '/build/blocks';
$manifest = __DIR__ . '/build/blocks-manifest.php';
// WP 6.8+: one-call convenience.
if ( function_exists( 'wp_register_block_types_from_metadata_collection' ) ) {
wp_register_block_types_from_metadata_collection( $build_dir, $manifest );
return;
}
// WP 6.7: index the collection, then loop and register each block from metadata.
if ( function_exists( 'wp_register_block_metadata_collection' ) ) {
wp_register_block_metadata_collection( $build_dir, $manifest );
$manifest_data = require $manifest;
foreach ( array_keys( $manifest_data ) as $block_type ) {
register_block_type_from_metadata( $build_dir . '/' . $block_type );
}
return;
}
// WP 5.5-6.6: no collection APIs; just loop the manifest directly.
if ( function_exists( 'register_block_type_from_metadata' ) ) {
$manifest_data = require $manifest;
foreach ( array_keys( $manifest_data ) as $block_type ) {
register_block_type_from_metadata( $build_dir . '/' . $block_type );
}
return;
}
}
add_action( 'init', 'register_blocks' );
/**
* Enqueues the block assets for the editor
*/
function enqueue_block_assets() {
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/editor-script.asset.php';
wp_enqueue_script(
'editor-script-js',
plugin_dir_url( __FILE__ ) . 'build/editor-script.js',
$asset_file['dependencies'],
$asset_file['version'],
false
);
}
add_action( 'enqueue_block_editor_assets', 'enqueue_block_assets' );
/**
* Enqueues the block assets for the frontend
*/
function enqueue_frontend_assets() {
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/frontend-script.asset.php';
wp_enqueue_script(
'frontend-script-js',
plugin_dir_url( __FILE__ ) . 'build/frontend-script.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
}
add_action( 'wp_enqueue_scripts', 'enqueue_frontend_assets' );