In WordPress, there is a function wp_get_sidebars_widgets() that spits out a multidimensional array of all registered sidebar ID’s with their children active widget IDs.

For context, this is a sample output:

Array
(
[wp_inactive_widgets] => Array
    (
    )

[sidebar-requests] => Array
    (
        [0] => widget-demographics-4
        [1] => recent-posts-7
        [2] => widget-newsletter-11
    )

[sidebar-team] => Array
    (
        [0] => widget-sidebar-list-6
        [1] => widget-donate-2
    )

)

I am using this code inside of a widget to find the ID of the sidebar it lives in. For example, there can only be one ‘widgets-newsletter-11’, and it lives inside the ‘sidebar-requests’ widget area.

I am having trouble storing the ID of this widget area. Here is a simple version of the code I am trying to use.

$this_widget_id = 'widget-newsletter-11';
$this_widget_area_id;

$all_widgets = wp_get_sidebars_widgets();

foreach ( $all_widgets as $widget_area => $widget_id ) {
    if ( $widget_id == $this_widget_id )
        $this_widget_area_id = $widget_area;
}

At the end of this, the stored variable, $this_widget_area_id remains empty.

What am I doing wrong?

Read more here: PHParray traversal


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: