Wordpress plugin development - get all active widg

2020-03-05 05:58发布

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

3条回答
Lonely孤独者°
2楼-- · 2020-03-05 06:32

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.

查看更多
你好瞎i
3楼-- · 2020-03-05 06:46

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.

查看更多
放荡不羁爱自由
4楼-- · 2020-03-05 06:49

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

查看更多
登录 后发表回答