I am creating a dynamic Block to display a widget area inside post. I coded the following in ES5. The block was registered successfully, but it has failed to render the I want. I have looked up a lot of tutorials, but I am not sure what I have missed.
php code:
function registerWidgetBlockArea() {
register_sidebar( array(
'name' => esc_html__( 'Widget Area for Widget Block', 'survivalisthk' ),
'id' => 'widget-block-area',
'description' => '',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
}
add_action( 'widgets_init', 'registerWidgetBlockArea');
//callback function for Widget Block
function createWidgetArea($attributes){
if ( is_active_sidebar( 'widget-block-area' ) ) {
echo '<div id="mobile-menu-widget-area" class="mobile-menu-widget widget-area" role="complementary">';
dynamic_sidebar( 'widget-block-area' );
echo '</div>';
}
echo 'Testing from callback function';
return 'Testing from callback function';
}
add_action('init', 'registerCustomBlocks');
function registerCustomBlocks() {
//Widget block for displaying widget
$block_path2 = '/widget_block.js';
wp_register_script(
'CUSTOM-block-widget',
plugins_url( $block_path2 , __FILE__ ),
[ 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor' ],
filemtime( plugin_dir_path( $block_path2 , __FILE__ ) )
);
register_block_type( 'custom-blocks/widget', array(
'editor_script' => 'CUSTOM-block-widget',
//'render_callback' => 'createWidgetArea',
) );
}
JS:
wp.blocks.registerBlockType( 'custom-blocks/widget', {
title: 'Widget Area',
icon: 'megaphone',
category: 'custom_block',
support: {
html: true,
reusable: false,
},
edit: function( props ) {
return (
el( ServerSideRender, {
block: 'custom-blocks/widget',
attributes: props.attributes,
} )
);
},
save: function( props ) {
return null;
}
} );
Thank you!