Title: Reusable Blocks
Last modified: May 26, 2019

---

# Reusable Blocks

 *  Resolved [jeffmeadows](https://wordpress.org/support/users/jeffmeadows/)
 * (@jeffmeadows)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/)
 * How can I make has_block (‘gallery’) work with a reusable block?
 * Thanks
    Jeff Meadows

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Corey McKrill](https://wordpress.org/support/users/coreymckrill/)
 * (@coreymckrill)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11589672)
 * Hi Jeff!
 * Could you give a little more context? Are you trying to use `has_block` as a 
   conditional in a template?
 *  Thread Starter [jeffmeadows](https://wordpress.org/support/users/jeffmeadows/)
 * (@jeffmeadows)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11589934)
 * 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.
 *  [Corey McKrill](https://wordpress.org/support/users/coreymckrill/)
 * (@coreymckrill)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11590080)
 * 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](https://github.com/WordPress/gutenberg),
   and I didn’t see anything that looked similar. So it might be worth opening an
   issue for this, probably as a “feature request”.
 *  Thread Starter [jeffmeadows](https://wordpress.org/support/users/jeffmeadows/)
 * (@jeffmeadows)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11590304)
 * 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.
 *  [Corey McKrill](https://wordpress.org/support/users/coreymckrill/)
 * (@coreymckrill)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11590309)
 * Sure thing! Glad to help.
 *  [Hozefa Saleh](https://wordpress.org/support/users/hozefasmile/)
 * (@hozefasmile)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11721904)
 * 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.
    -  This reply was modified 6 years, 9 months ago by [Hozefa Saleh](https://wordpress.org/support/users/hozefasmile/).

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Reusable Blocks’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [Hozefa Saleh](https://wordpress.org/support/users/hozefasmile/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/reusable-blocks/#post-11721904)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
