I am creating a custom block name section, want to make it more power full with adding settings with different css properties.
But stuck at the allowing blocks inside this custom block as column block does.
here is what I done:
// All blocks located here
if( !defined( 'WP_BLOCKS_URL' ) ) {
define( 'WP_BLOCKS_URL', get_template_directory_uri() . '/blocks/' );
}
// Register Gutenberg blocks
add_action( 'init', 'wp_register_gutenberg_blocks_assets' );
function wp_register_gutenberg_blocks_assets() {
if( ! function_exists( 'register_block_type' ) ) {
// Gutenberg is not active.
return;
}
// Register
register_block_type( 'custom/section', array(
'editor_script' => 'wp-section-block-script',
) );
}
// Manage editor scripts
add_action( 'enqueue_block_editor_assets', 'wp_custom_gutenberg_scripts' );
function wp_custom_gutenberg_scripts() {
if( ! function_exists( 'register_block_type' ) ) {
// Gutenberg is not active.
return;
}
// Section block script
wp_register_script(
'wp-section-block-script', // Handle.
WP_BLOCKS_URL . 'section/block.js',
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ), // Dependencies, defined above.
WP_BLOCKS_URL . 'section/block.js',
true
);
}
// block.js
var el = wp.element.createElement;
var registerBlockType = wp.blocks.registerBlockType;
var InnerBlocks = wp.editor.InnerBlocks;
var BlockControls = wp.editor.BlockControls;
var ALLOWED_BLOCKS = ['core/paragraph'];
registerBlockType( 'custom/section', {
title: 'Custom Section',
description: '',
icon: 'ANY-ICON',
category: 'layout',
edit: function() {
return [
el(BlockControls, { key: 'controls' },
el('div', { className: 'custom-sec-inner' },
el(InnerBlocks, {
allowedBlocks: ALLOWED_BLOCKS
} )
)
),
];
},
save: function(props) {
return [
el('div', { className: 'custom-sec-block' },
el('div', { className: 'custom-sec-inner' },
el( InnerBlocks.Content )
)
),
];
}
} );
It does not work, event doesn't give any error and not even element is added to the gutenberg builder.
Please help with appropriate solution.