Hi
I have been testing and found it is difficult to pass a unique ID for my carousel library. The library targets a unique DIV ID so each instance of my block will require this ID.
I have included my JS file inside the enqueue_assets and used 'wp_add_inline_script' to send the unique ID to this.
My problem is with 'render_template' as I cannot send the same unique ID across. I have tried using the attributes inside 'render_template' but if I do this then I cant send this across to JS.
I have tried various options but no luck, below is just an example so you get the idea of what I am trying to achieve. I was just wondering if I was missing something obvious?
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Carousel Block',
'id' => 'dc-carousel-block',
'description' => 'Carousel with product support',
'type' => 'block',
'icon' => 'screenoptions',
'category' => 'dc-blocks',
'context' => 'side',
'render_template' => __DIR__ . '/template/dc-carousel-block-template.php',
'enqueue_style' => plugin_dir_url( __FILE__ ) . 'css/splide.min.css',
'enqueue_script' => plugin_dir_url( __FILE__ ) . 'js/splide.min.js',
'supports' => [
'align' => ['wide', 'full'],
],
'enqueue_assets' => function() {
wp_enqueue_script( 'splide-custom-script', plugin_dir_url( __FILE__ ) . 'js/splide-run.js', [], '1', true );
wp_add_inline_script( 'splide-custom-script', 'const splideSettingsPHP = ' . json_encode( array(
'uniqueTargetID' => $uniqueID,
'is_preview' => $is_preview,
'autoplay' => $autoplay,
) ), 'before' );
},
'keywords' => ['carousel', 'product', 'listing', 'ecommerce'],
'multiple' => false,
'reusable' => false,
'preview' => [
'image' => '', // Image ID
'title' => 'Carousel',
'description' => 'Carousel with product support',
],
'fields' => [
[
...