1

As widgets are outside post content, i couldn't find the way to parse blocks inserted inside widget area for ex. "sidebar-1"

We use something like below for parsing post contents,

$post = get_post('100');
parse_blocks($post->post_content) 

But for widgets, i couldn't do it like below,

ob_start();
dynamic_sidebar('sidebar-1');
$widget_content = ob_get_clean();
parse_blocks($widget_content) 
4
  • how are you inserting blocks inside a widget? If you mean the entire widget area, you want the precursor that's processed by dynamic_sidebar, not the end result. What are you trying to do that requires this? There are filters for modifying or inspecting blocks when they're rendered
    – Tom J Nowell
    Nov 18, 2021 at 15:11
  • I meant inside each sidebar (widget location). for ex. i have registered sidebar named "sidebar-1" and also having heading, paragraph & image block inside that sidebar. Now i want to get the attributes of those blocks for processing some backend operation inside functions.php. Hope i explained better. Nov 18, 2021 at 16:29
  • what's the backend processing you're trying to do?
    – Tom J Nowell
    Nov 18, 2021 at 22:29
  • For ex. If the attribute "fontSize" is above 40px, i want to change sidebar width. Nov 19, 2021 at 3:21

1 Answer 1

1

I came across a similar use case where I needed to parse block widgets inside available widget areas. I opened an issue on the Gutenberg Repository. After some links and suggestions, I was able to write this code which can parse blocks inside available widget areas:

// Get all available widgets in the widget areas
$widgets = wp_get_sidebars_widgets();

if ( isset( $widgets ) && is_array( $widgets ) ) {
    
    global $wp_widget_factory;
    
    $blocks_array = [];
    
    foreach( $widgets as $key => $value ) {
        
        // Skip for inactive widgets
        if ( $key == 'wp_inactive_widgets' ) {
            continue;
        }
        
        if ( isset( $value ) && is_array( $value ) && ! empty( $value ) ) {
            
            foreach( $value as $widget_id ) {
                
                $parsed_id = wp_parse_widget_id( $widget_id );
                $widget_object = $wp_widget_factory->get_widget_object( $parsed_id['id_base'] );

                if ( $widget_object && isset( $parsed_id['number'] ) ) {
                    $all_instances                   = $widget_object->get_settings();
                    $instance                        = $all_instances[ $parsed_id['number'] ];
                    $serialized_instance             = serialize( $instance );
                    $prepared['instance']['encoded'] = base64_encode( $serialized_instance );
                    $prepared['instance']['hash']    = wp_hash( $serialized_instance );

                    if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) {
                        // Use new stdClass so that JSON result is {} and not [].
                        $prepared['instance']['raw'] = empty( $instance ) ? new stdClass : $instance;
                        $blocks_array[] = parse_blocks( $prepared['instance']['raw']['content'] );
                    }                                
                }
            } // inner foreach
        } // isset $value
    } //foreach
    
    // This is the complete blocks data with attributes and other settings
    print_r( $blocks_array );

} // all widgets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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