1

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?

2
  • Are you putting $sidebar options = array(); after the foreach loop or is it a typo? If you leave it in that position its value will be set empty.
    – iEmanuele
    Aug 20, 2013 at 7:56
  • 1
    It doesn't work when its before it either
    – Cptn
    Aug 20, 2013 at 8:47

1 Answer 1

0

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()
                )
            )
        ); 
12
  • So that sort of worked, I put in the function and when to test it, which returned Array ( [0] => Array ( [name] => Primary Widget Area [value] => primary ) [1] => Array ( [name] => Homepage Widget Area [value] => home ) ) Which is what I want but call the function in the meta box code did not work, still absolute blank. Also when i attempted to print $sidebar_options that the function returns, nothing happened
    – Cptn
    Aug 20, 2013 at 9:23
  • what do you see if you "firebug" the select tag?
    – iEmanuele
    Aug 20, 2013 at 9:55
  • Nothing out of the ordinary tbh. Take a look at my edit, what do think? I just cant seem to get it to call $GLOBALS['wp_registered_sidebars']
    – Cptn
    Aug 20, 2013 at 10:03
  • try global $wp_registered_sidebars; that effort.. : )
    – iEmanuele
    Aug 20, 2013 at 10:06
  • Thanks. Where am I going wrong global $wp_registered_sidebars; /*$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', );*/ $sidebar_options = array(); foreach ($wp_registered_sidebars as $sidebar_op ){ $sidebar_options[] = array( 'name' => $sidebar_op['name'], 'value' => $sidebar_op['id'] ); } I placed it right before I call the metabox
    – Cptn
    Aug 20, 2013 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.