Title: Using parse_blocks() on a widget?
Last modified: June 13, 2022

---

# Using parse_blocks() on a widget?

 *  [Steve](https://wordpress.org/support/users/bugdens/)
 * (@bugdens)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/using-parse_blocks-on-a-widget/)
 * Hello,
 * I’ve developed a Gutenberg block and I’m trying to access the block details from
   PHP, specifically the nested attributes.
 * I found that I can do this using parse_blocks i.e.
    [https://developer.wordpress.org/reference/functions/parse_blocks/](https://developer.wordpress.org/reference/functions/parse_blocks/)
 * So I can use the following to return the details for a page/post:
    $blocks = 
   parse_blocks( $post->post_content );
 * However this only works if the block is placed on a page or post, it won’t work
   on say a widget because the blocks retrieved are on the page not the widget.
 * So how can I access the blocks on a widget?
 * This question is related to another one I posted on accessing nested attributes:
   
   [https://wordpress.org/support/topic/accessing-nested-attributes-in-a-wordpress-gutenberg-block-via-php/](https://wordpress.org/support/topic/accessing-nested-attributes-in-a-wordpress-gutenberg-block-via-php/)
   I decided to create a new topic as I can’t imagine many people would read through
   such a long set of replies.
 * Steve

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

 *  [faridjc](https://wordpress.org/support/users/faridjc/)
 * (@faridjc)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/using-parse_blocks-on-a-widget/#post-15771175)
 * Hi Steve!
 * If I understand correctly, you’re trying to access content of a post outside 
   of the loop, in order to retrieve the blocks out of the content string.
 * I haven’t try this myself, but I suggest you use this code:
 *     ```
       global $post;
       $post_id = $post->ID;
       $post_content = get_the_content( $post_id );
       $blocks = parse_blocks( $post_content );
       ```
   
 * Try with the code above and let us know how it went.
 * Here are some documentation references about retrieving post content that might
   be helpful to you:
 * [https://developer.wordpress.org/reference/functions/get_the_content/](https://developer.wordpress.org/reference/functions/get_the_content/)
   
   [https://developer.wordpress.org/reference/functions/get_post/](https://developer.wordpress.org/reference/functions/get_post/)
 * Best regards,
 * Farid.
 *  Thread Starter [Steve](https://wordpress.org/support/users/bugdens/)
 * (@bugdens)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/using-parse_blocks-on-a-widget/#post-15771831)
 * Hello Farid,
 * Thank you so much for your reply and suggestion.
 * What I’m trying to do is to access the attributes for my dynamic block which 
   contains child blocks.
 * I can access the top level attributes from my block from the $attributes variable
   as follows:
    $attributes[‘SomeAttribute’]
 * I don’t know how to directly access the attributes for the child blocks.
 * The above code retrieves the content for the page/post, so I can iterate through
   the structure and identify my block, then extract it’s attributes including the
   nested attributes.
 * Though it’s not a very efficient way to obtain the attributes as my block can
   exist inside a row/column block and so each instance of my block needs to read
   the content for the whole page/post each time and then navigate through the structure
   recursively to find possible instances of my block and identify the correct one
   before extracting it’s attributes. However, at least it works when the block 
   is on a page or post.
 * I also want to be able to place my block on a widget but now it won’t work as
   the parse_blocks() retrieves the values for the page/post not a widget.
 * So I suppose there are really two questions:
    1) How can I access the attributes
   for child blocks directly? 2) If 1) is not possible, how can I read the content
   of a widget if the block actually on a widget.
 * I’m not sure if my block might need to exist in other places but so far I need
   it for pages, posts and widgets. Could there be other places where it might exist
   I wonder?
 * Hope that’s clear, it’s tricky to explain.
 * Steve

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

The topic ‘Using parse_blocks() on a widget?’ is closed to new replies.

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [gutenberg](https://wordpress.org/support/topic-tag/gutenberg/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [Steve](https://wordpress.org/support/users/bugdens/)
 * Last activity: [3 years, 11 months ago](https://wordpress.org/support/topic/using-parse_blocks-on-a-widget/#post-15771831)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
