How do you List all Sidebars in a Metabox

I know this question has been answered many times before, including here where i came from so it'll probably be real simple.

I've registered my sidebars as normal

function wmw_sidebars() {
    // Register default widgetized areas
    $sidebars = array(
        'primary' = array(
            'name' = __( 'Primary Widget Area', WMW_ADMIN_TEXTDOMAIN ),
            'desc' = __( 'The primary widget area', WMW_ADMIN_TEXTDOMAIN )
        ),
        'home' = array(
            'name' = __( 'Homepage Widget Area', WMW_ADMIN_TEXTDOMAIN ),
            'desc' = __( 'The homepage widget area', WMW_ADMIN_TEXTDOMAIN )
        ),
    );

    foreach ( $sidebars as $type = $sidebar ){
        register_sidebar(array(
            'name' = $sidebar['name'],
            'id'= $type,
            'description' = $sidebar['desc'],
            'before_widget' = 'div id="%1$s" class="widget %2$s"',
            'after_widget' = '/div',
            'before_title' = 'h4 class="widgettitle"span',
            'after_title' = '/span/h4',
        ));
    }
}

I'm using the same CMB Metabox Class as fshequin and have the metaboxes showing up in in my posts and pages.

    $meta_boxes[] = array(
    'id' = 'WMW_page_layout_meta',
    'title' = __( 'Page Layout Options', WMW_ADMIN_TEXTDOMAIN ),
    'pages' = get_post_types(),
    'context' = 'side',
    'priority' = 'default',
    'show_names' = false, // Show field names on the left
    'fields' = array(
        array(
            'name' = __( 'Page Layout', WMW_ADMIN_TEXTDOMAIN ),
            'desc' = __( 'You can choose between a left, right, or no sidebar layout for your page.', WMW_ADMIN_TEXTDOMAIN ),
            'id' = '_layout',
            'type' = 'select',
            'options' = array(
                array('name' = 'Full Width (No Sidebar)', 'value' = 'full_width', ),
                array('name' = 'Sidebar on Left, Content on Right', 'value' = 'left_sidebar', ),
                array('name' = 'Sidebar on Right, Content on Left', 'value' = 'right_sidebar',)
            )
        ),
        array(
            'name' = __( 'Page Sidebar', WMW_ADMIN_TEXTDOMAIN ),
            'desc' = __( 'Choose the Sidebar to show on this page.', WMW_ADMIN_TEXTDOMAIN ),
            'id'   = '_page_sidebar',
            'type' = 'select',
            'options' = $sidebar_options
        )
    )
);

But for whatever the reason I cannot seem to get the select options to show.

I Placed the array in the same functions file as registering the sidebars but not in a function.

$sidebars = $GLOBALS['wp_registered_sidebars'];

foreach ( $sidebars as $sidebar ){
    $sidebar_options[] = array(
        'name'  = $sidebar['name'],
        'value' = $sidebar['id']
    );
}
$sidebar_options = array();

Whatever I try, it is not happening, I changed the variable names to avoid conflict with sidebar register array but no dice.

Any help would be MUCH APPRECIATED!

------------------------ EDITED ---------------------------------------

Ok I broke it down and went to test each part of it.

$sidebar_ops = array(); 
$sidebar_ops[] = array(
    'name' = __( 'Page Sidebar 1', WMW_ADMIN_TEXTDOMAIN ),
    'value' = 'sidebar_1',
    );
$sidebar_ops[] = array(
    'name' = __( 'Page Sidebar 2', WMW_ADMIN_TEXTDOMAIN ),
    'value' = 'sidebar_2',
    );

  //$GLOBALS['wp_registered_sidebars']
  $sidebar_options = array();
  foreach ( $sidebar_ops as $sidebar_op ){
    $sidebar_options[] = array(
        'name'  = $sidebar_op['name'],
        'value' = $sidebar_op['id']
    );
  }

I compiled a bogus array and then used the foreach to test that.

The problem appears to be with $GLOBALS['wp_registered_sidebars'] and using that array. What the proper way to call this?

Topic list metabox sidebar Wordpress

Category Web


You can also use a function like this:

        <?php

           /* Other stuff */

            function sidebarOptions(){
              $sidebars = $GLOBALS['wp_registered_sidebars'];
              $sidebar_options = array();
              foreach ( $sidebars as $sidebar ){
                $sidebar_options[] = array(
                    'name'  => $sidebar['name'],
                    'value' => $sidebar['id']
                );
              }
              return $sidebar_options;
            }

           /* Other stuff */

/*
 * Trying to print the result to check if it is what expect us
 * echo '<pre>';
 * print_r( sidebarOptions() );
 * echo '</pre>';
 */         
           $meta_boxes[] = array(
            'id' => 'WMW_page_layout_meta',
            'title' => __( 'Page Layout Options', WMW_ADMIN_TEXTDOMAIN ),
            'pages' => get_post_types(),
            'context' => 'side',
            'priority' => 'default',
            'show_names' => false, // Show field names on the left
            'fields' => array(
                array(
                    'name' => __( 'Page Layout', WMW_ADMIN_TEXTDOMAIN ),
                    'desc' => __( 'You can choose between a left, right, or no sidebar layout for your page.', WMW_ADMIN_TEXTDOMAIN ),
                    'id' => '_layout',
                    'type' => 'select',
                    'options' => array(
                        array('name' => 'Full Width (No Sidebar)', 'value' => 'full_width', ),
                        array('name' => 'Sidebar on Left, Content on Right', 'value' => 'left_sidebar', ),
                        array('name' => 'Sidebar on Right, Content on Left', 'value' => 'right_sidebar',)
                    )
                ),
                array(
                    'name' => __( 'Page Sidebar', WMW_ADMIN_TEXTDOMAIN ),
                    'desc' => __( 'Choose the Sidebar to show on this page.', WMW_ADMIN_TEXTDOMAIN ),
                    'id'   => '_page_sidebar',
                    'type' => 'select',
                    'options' => sidebarOptions()
                )
            )
        ); 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.