Support » Developing with WordPress » Reusable Blocks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi Jeff!

    Could you give a little more context? Are you trying to use has_block as a conditional in a template?

    Hi Corey. Thanks for getting back to me. I am indeed using has_block as a conditional in my templates. I want to be able to label posts on blog pages to show when the post has a gallery.

    has_block('gallery') works fine under normal circumstances but when the gallery block is set to be reusable its name becomes ‘block’ and there appears to be no way to tell that it’s a gallery. At the moment the gallery is the only reusable block in each post so I’m using has_block('block') and that’s working fine, but it would be helpful if reusable blocks could be targeted based on their type.

    Perhaps in the future the reusable block name could include the original block name in some form.

    Interesting! I don’t know a ton about the inner workings of reusable blocks, but my understanding is that they are essentially posts of a custom post type.

    If you switch your editor to Code Editor mode, you can see the block grammar. With a normal gallery block it will begin with something like:

    <!-- wp:gallery {"ids":[9028,9262,129]} -->

    So essentially all that the has_block function is doing is checking if the string you provide matches the part after wp:.

    When you have a reusable block, it looks something like this:

    <!-- wp:block {"ref":12821} /-->

    Where the ref is the post ID of the reusable block. If you have access to your database tables, you can look up that post ID in the wp_posts table and you’ll see that it basically just contains the grammar of your original block, beginning with <!-- wp:gallery {"ids":[9028,9262,129]} -->.

    So, I haven’t tried this to see if it would work, but one thing you could test is including the ref part in your conditional. Something like:

    has_block( 'block {"ref":12821}' )

    (That assumes you are doing this in a situation where you can add the reusable block and then add the correlating post ID to your template afterwards)

    Perhaps in the future the reusable block name could include the original block name in some form.

    That sounds reasonable to me. Or alternatively, maybe the has_block test could also check against the name you give the reusable block? I did a quick search through the open issues for Gutenberg on GitHub, and I didn’t see anything that looked similar. So it might be worth opening an issue for this, probably as a “feature request”.

    Thanks Corey that’s very helpful.

    I guess the difficulty in my case is that the ref or post ID for reusable blocks identifies the individual reusable block but not the original block type. My template just needs a simple conditional test for block type within the post loop. In practice I doubt that I’ll need more than one reusable block per post anyway so the conditional has_block( 'block' ) works quite well. I’m also testing for get_post_gallery() to pick up those galleries that have not been made into reusable blocks and has_block( 'core-embed/vimeo' ) to flag up my videos.

    I’ll take a look at Gutenberg on GitHub with a view to putting in a feature request. Thanks for your help.

    Sure thing! Glad to help.

    hozefasmile

    (@hozefasmile)

    Hi All,

    I want to focus this topic again for one who using reusable blocks and wan’t to utilize conditional function has_block for it using an example case.

    In my case I am using many reusable blocks on individual pages. One of the custom block I created loads a svg sprite file for showing some svg icons based on class name. So that particular reusable block loads an about 200kb of file which have lots of svg icons. So anyway its not a good idea to load these icons in other pages which are not showing that reusable custom block ( its a waste of kb for those pages – cause slow speed loading of other pages), So I have applied has block in this way

    <?php 
    	$id = get_the_ID();
    	if( has_block('lazyblock/uni-why-choose-section', $id ) || has_block('block {"ref":341}', $id ) ){
    // add code for custom loading anything, in my case I included svg sprite file.
    }
    	?>

    In this case ‘uni-why-choose-section’ is a custom block , you might use any default block like gallery too, and “ref”:341 is the id of reusable block created using that same custom block. Hope this will be useful for someone who are thinking on how to use has_block condition with reusable blocks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.