I've tried the parse_blocks() function, however it returns all of the blocks. I'm needing to narrow it down to just the current block's ID. Is there a PHP function for that?

I'm needing get the unique ID for the block so I can set a transient.


You can iterate on your parsed blocks to get the right block.

$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $key => $block ) {
    if ( 'block_xxxxx' === $block[ 'id' ] ) {
        // do some action here
  • The block type/name gets used several times on the page, so that would only narrow it down to a collection of those blocks for me. I'm trying to obtain the ID so I can refer to the block directly. – David Walz Oct 14 '20 at 13:15
  • How do you know which block you want to target if you have it multiple times on your page? – Tim Oct 14 '20 at 17:08
  • I need each block to be able access its own unique ID in the PHP. I see that WP generates one, but I'm not sure if there is way to get that on the frontend. I might have to generate it with uniqid() , however that presents its own set of issues. – David Walz Oct 14 '20 at 19:01
  • I still don't understand how you will know which ID is the one you want to target as they are dynamically generated. What do you want to do with the targeted block? – Tim Oct 15 '20 at 9:34
  • I think the point here is that this method doesn't exist. I was looking for something similar to the get_the_ID() function, however it appears this generated ID changes each time the block is saved. Which makes this approach pointless. Closing down the question. – David Walz Oct 19 '20 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.