最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get options of all active widgets ?

programmeradmin1浏览0评论

I am currently using a loop that goes trough all sidebars looking for widgets than loop those again in order to get all widgets options.

Doing this on

add_filter('sidebars_widgets', array($this, 'sidebars_widgets'));

filter and it just does not seem right that I need to loop this much just to get all options

I took this example from display widgets plugin and it just does not seem right

function sidebars_widgets($sidebars) {    
    if ( is_admin() ) {
        return $sidebars;
    }

    global $wp_registered_widgets;

    foreach ( $sidebars as $s => $sidebar ) {
        if ( $s == 'wp_inactive_widgets' || strpos($s, 'orphaned_widgets') === 0 || empty($sidebar) ) {
            continue;
        }

        foreach ( $sidebar as $w => $widget ) {
            // $widget is the id of the widget
            if ( !isset($wp_registered_widgets[$widget]) ) {
                continue;
            }

                $opts = $wp_registered_widgets[$widget];
                $id_base = is_array($opts['callback']) ? $opts['callback'][0]->id_base : $opts['callback'];

                if ( !$id_base ) {
                    continue;
                }

                $instance = get_option('widget_' . $id_base);

                if ( !$instance || !is_array($instance) ) {
                    continue;
                }

                if ( isset($instance['_multiwidget']) && $instance['_multiwidget'] ) {
                    $number = $opts['params'][0]['number'];
                    if ( !isset($instance[$number]) ) {
                        continue;
                    }

                    $instance = $instance[$number];
                    unset($number);
                }

                unset($opts);

                print_r($instance);// here they are



            unset($widget);
        }
        unset($sidebar);
    }

    return $sidebars;
}

would you know a better , faster way to do this ?

I am currently using a loop that goes trough all sidebars looking for widgets than loop those again in order to get all widgets options.

Doing this on

add_filter('sidebars_widgets', array($this, 'sidebars_widgets'));

filter and it just does not seem right that I need to loop this much just to get all options

I took this example from display widgets plugin and it just does not seem right

function sidebars_widgets($sidebars) {    
    if ( is_admin() ) {
        return $sidebars;
    }

    global $wp_registered_widgets;

    foreach ( $sidebars as $s => $sidebar ) {
        if ( $s == 'wp_inactive_widgets' || strpos($s, 'orphaned_widgets') === 0 || empty($sidebar) ) {
            continue;
        }

        foreach ( $sidebar as $w => $widget ) {
            // $widget is the id of the widget
            if ( !isset($wp_registered_widgets[$widget]) ) {
                continue;
            }

                $opts = $wp_registered_widgets[$widget];
                $id_base = is_array($opts['callback']) ? $opts['callback'][0]->id_base : $opts['callback'];

                if ( !$id_base ) {
                    continue;
                }

                $instance = get_option('widget_' . $id_base);

                if ( !$instance || !is_array($instance) ) {
                    continue;
                }

                if ( isset($instance['_multiwidget']) && $instance['_multiwidget'] ) {
                    $number = $opts['params'][0]['number'];
                    if ( !isset($instance[$number]) ) {
                        continue;
                    }

                    $instance = $instance[$number];
                    unset($number);
                }

                unset($opts);

                print_r($instance);// here they are



            unset($widget);
        }
        unset($sidebar);
    }

    return $sidebars;
}

would you know a better , faster way to do this ?

Share Improve this question asked May 18, 2015 at 12:31 BennBenn 1,0631 gold badge15 silver badges32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

This actually seems like a quite plausible approach - to me.

There is nothing wrong with looping through it, sometimes it is the only/fastest way anyway.

I don't know how you get your sidebars, but for this

$sidebars_widgets = get_option( 'sidebars_widgets' );

can be used. Which gives you an associative array containing a list of widgets per sidebar and a list of all inactive widgets.

This function may help:

function get_all_widgets_data($sidebar_id=null){
    $result = [];
    $sidebars_widgets = get_option('sidebars_widgets');

    if(is_array($sidebars_widgets)){

        foreach ($sidebars_widgets as $key => $value) {

            if(is_array($value)){
                foreach ($value as $widget_id) {
                    $pieces = explode('-', $widget_id);
                    $multi_number = array_pop($pieces);
                    $id_base = implode('-', $pieces);
                    $widget_data = get_option('widget_' . $id_base);
                    
                    // Remove inactive widgets 
                    if($key != 'wp_inactive_widgets' ) {
                        unset($widget_data['_multiwidget']);
                        $result[$key] = $widget_data;
                    }
                }
            }
        }
    }
    if($sidebar_id){
        return isset($result[$sidebar_id]) ? $result[$sidebar_id] : [] ;
    }
    return $result;
}
发布评论

评论列表(0)

  1. 暂无评论