• I have created a new block and enqueued a css file for it like this

    $id = get_the_ID();
    if(has_block('lazyblock/uni-ybox',$id)){
    wp_register_style('uniyboxstyle', get_template_directory_uri() . '/css/yBox.min.css', array('unibsstyle'), null, 'all');
    wp_enqueue_style('uniyboxstyle'); // Enqueue it!
    }

    unibsstyle is a dependency style. Now it works fine and load css file only when that particular block is being used on any page or post.

    But problem happen when we put that block inside a reusable block. The has_block condition stop working. and I have to change the condition to something like this
    if(has_block('lazyblock/uni-ybox',$id) || has_block('block {"ref":341}', $id ) ){
    Where 341 is the id of the reusable block created. But this is not an ideal way to do this.

    Either blocks enqueued files should be supported directly by the reusable blocks or there should be some standard has_reusable_block function condition to allow enqueue files when block used in any reusable block created.

Viewing 1 replies (of 1 total)
  • Plugin Author Daniel Richards

    (@talldanwp)

    The has_block function is quite rudimentary, it just runs a regular expression on the post content.

    In the case of a reusable block, the reusable block’s content isn’t part of the post itself. The reusable block is a post type with its own content and that gets dynamically inserted when your post is viewed.

    You could probably also do has_block('lazyblock/uni-ybox',341) to check the reusable block content.

    To make it work dynamically would require building an array of all reusable block post ref/ids in a post, and then running has_block on each of those.

Viewing 1 replies (of 1 total)
  • The topic ‘Block enqueued files not work with reusable block’ is closed to new replies.