2

I'm used to check if a widget from installed my plugin is active.

With the new block editor extending to widgets in WordPress 5.8, I was wondering if there is a way to check the widget block that I created (both old php-based and gutenberg-based widgets are installed by my plugin) is activated by the user.

Thanks!

1 Answer 1

3

One could e.g. get the widget blocks data with:

$widget_blocks = get_option( 'widget_block' );

and then loop through the array and run has_block() on the contant part, e.g. like (untested):

function is_active_block_widget_wpse( $blockname )
    $widget_blocks = get_option( 'widget_block' );
    foreach( (array) $widget_blocks as $widget_block ) {
        if ( ! empty( $widget_block['content'] ) 
             && has_block( $blockname, $widget_block['content'] ) 
        ) {
            return true;
        }
    }
    return false;
}

to find if a given blockname is an active widget:

is_active_block_widget_wpse( 'my-plugin/my-block' )

We note that this simple implementation assumes none inactive block widgets.

Other helpful functions: wp_get_sidebars_widgets() and is_active_widget()

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.