WordPress plugin development – get all active widgets

I am developing a wordpress plugin that creates a widget that would act upon another widget. I have searched but cant seem to find (if it exists) a hook that would give details of all active instances of widgets. any help would be appreciated if you have come across this. thanks

Related posts

Leave a Reply

3 comments

  1. get_option('sidebars_widgets') gives you an associative array that contains a list of widgets for each sidebar plus a list of all inactive widgets.

    get_option('widget_widgetname') will give you an associative array that contains the settings of all instances of your widget.

  2. for example to remove a widget from a page

    add_filter( 'sidebars_widgets', 'disable_widgets' );
    
    function disable_widgets( $sidebars_widgets ) {
    global $qode_options_proya;
        //print_r($sidebars_widgets);//gives a list of widgets
        if(is_admin()){return $sidebars_widgets;}
        if(get_post_meta( get_the_ID(), 'hide_allwidgets_checkbox', true )=="on"){return false; }
    
    
        if(get_post_meta( get_the_ID(), 'hide_footer', true )=="on"){unset($sidebars_widgets["footer_column_1"]);}
        if(get_post_meta( get_the_ID(), 'hide_topmenu_checkbox', true )=="on"){unset($sidebars_widgets["header_top"]); }
        return $sidebars_widgets;
    }
    

    This might vary depending on your theme, so use print_r($sidebars_widgets); to verify the widgets available on your case

  3. You can get all the active widgets of sidebar as follows::

    $sidebars_widgets = get_option( 'sidebars_widgets' );
    

    it will give you an associative array containing a list of widgets per sidebar and a list of all inactive widgets.