WordPress – sidebars list

I’m trying to get a list with all registered sidebars using $wp_registered_sidebars but it returns an empty array.

Here is my code:

Read More
foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
    $sidebar_options[$sidebar['id']] = $sidebar['name'];
}

It returns Array()

In a page template it works, but not in my functions.php

Any idea?
Thanks

Related posts

Leave a Reply

2 comments

  1. put this function in your theme functions.php

        function get_my_widgets()
        {
        foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
        {
            $sidebar_options[$sidebar['id']] = $sidebar['name'];
        }
        }
    
    add_action('init','get_my_widgets');
    

    and call this function as usual as get_my_widgets(); to get the registered sidebar list

  2. add the following function in your theme functions.php

    <?php
    function my_sidebar_selectbox( $name = '', $current_value = false ) {
        global $wp_registered_sidebars;
        if ( empty( $wp_registered_sidebars ) )
            return;
         foreach ( $wp_registered_sidebars as $sidebar ) :
         echo $sidebar['name'];
         endforeach; 
    
    }
    ?>
    

    and call it as my_sidebar_selectbox(); it is working to me