WordPress: Alternative to wp_get_sidebars_widgets for theme development

wp_get_sidebars_widgets() is very usefull to get infos about widgets in a sidebar. However, the WP codex states it’s a private function, not to be used for theme development… which is specifically my case. What are the alternatives? How to get widgets infos without it? I guess with dynamic_sidebar() and some PHP but I don’t see how, really.

What I want to get is:

Read More
  • the widgets ids (which informs me what widget has been used).
  • the widgets data entered by the user in the form.

Thanks!

Related posts

Leave a Reply

1 comment

  1. Ok, so I found a solution, but it looks like a hack. If someone knows something better…

    My sidebar’s id is ‘homepage’ and all my widget ids start with my prefix, nd_home_:

    global $wp_registered_sidebars, $wp_registered_widgets;
    
    ob_start();
    dynamic_sidebar('homepage'); //my sidebar id is 'homepage'
    $sidebar_contents = ob_get_clean();
    
    $widgetidspart1 = explode('nd_home_',$sidebar_contents); //my widgets ids start with nd_home_
    for ($i = 1; $i < count($widgetidspart1); $i++) {
        $widgetidspart2 = explode('" ', $widgetidspart1[$i] );
        $widgetids[] = $widgetidspart2[0]; //id without nd_home_
    }
    
    //add nd_home_ to every widget id
    foreach ( $widgetids as $id) {
        $widgetids_total[] = 'nd_home_' . $id;
    }
    
    //now I have all the ids in $widgetids_total. I can get the widget data:
    
    foreach( $widgetids_total as $id ) {
    
        $option_name = $wp_registered_widgets[$id]['callback'][0]->option_name;
    
        $key = $wp_registered_widgets[$id]['params'][0]['number'];
        $widget_data = get_option($option_name);
    
        $data[] = $widget_data[$key];
    }
    
    //$data contains my widgets data