How to List all Sidebars in a Metabox

I’m trying to add a metabox to every page that is a drop down list of all registered sidebars that is then saved in a custom field.

I’m using this metabox class

Read More

I’ve gotten as far as this:

// get registered Sidebars
$sidebars = $GLOBALS['wp_registered_sidebars'];
foreach ( $sidebars as $sidebar ) { 
   $sidebar_options[$sidebar['name']] = $sidebar['name'];
}

Then I call the $sidebars variable in my metabox declaration like so:

$meta_boxes[] = array(
    'id'         => 'page_metabox',
    'title'      => 'Page Custom Fields',
    'pages'      => array( 'page', ), // Post type
    'context'    => 'normal',
    'priority'   => 'high',
    'show_names' => true, // Show field names on the left
    'fields'     => array(
        array(
            'name' => 'Page Sidebar',
            'desc' => 'Choose the Sidebar to show on this page.',
            'id'   => /*$prefix .*/ 'erh_page_sidebar',
            'type' => 'select',
            'options' => $sidebar_options,  

        )

    ) 

);

This is the result of $sidebar_options:

$sidebar_options = array (
 'Header Sidebar' => 'Header Sidebar',
 'Main Sidebar' => 'Main Sidebar',
 'News Sidebar' => 'News Sidebar',
 'Home Panel 1' => 'Home Panel 1',
 'Home Panel 2' => 'Home Panel 2',
 'Home Panel 3' => 'Home Panel 3',
 'Footer 1 Sidebar' => 'Footer 1 Sidebar',
 'Footer 2 Sidebar' => 'Footer 2 Sidebar',
 'Footer 3 Sidebar' => 'Footer 3 Sidebar',
 'Footer 4 Sidebar' => 'Footer 4 Sidebar',
 'Footer 5 Sidebar' => 'Footer 5 Sidebar',
 'Footer 6 Sidebar' => 'Footer 6 Sidebar',
 'Right Sidebar 1' => 'Right Sidebar 1',
 'Staff login Sidebar' => 'Staff login Sidebar',
 'Staff Sidebar' => 'Staff Sidebar',
 'Careers Sidebar' => 'Careers Sidebar',
 'Care Providers Sidebar' => 'Care Providers Sidebar',
  )

my problem is that this metabox class wants the drop down options to be in this form and I don’t know how to do that:

array( 'name' => 'Sidebar 1', 'value' => 'Right Sidebar 1', ),
array( 'name' => 'Sidebar 2', 'value' => 'Right Sidebar 2', ),
array( 'name' => 'Sidebar 3', 'value' => 'Right Sidebar 3', ),

Any help is much appreciated 🙂

Related posts

1 comment

  1. Try this:

    <?php
        $sidebar_options = array();
        $sidebars = $GLOBALS['wp_registered_sidebars'];
    
        foreach ( $sidebars as $sidebar ){
            $sidebar_options[] = array(
                'name'  => $sidebar['name'],
                'value' => $sidebar['id']
            );
        }
    ?>
    

    This prints out( with my registered sidebars ):

    Array
    (
        [0] => Array
            (
                [name] => Language Menu
                [value] => sidebar-1
            )
    
        [1] => Array
            (
                [name] => Primary Sidebar
                [value] => primary-sidebar
            )
    
        [2] => Array
            (
                [name] => Secondary Sidebar
                [value] => secondary-sidebar
            )
    
        [3] => Array
            (
                [name] => Footer left box
                [value] => footer_box_left
            )
    
        [4] => Array
            (
                [name] => Footer middle box
                [value] => footer_box_middle
            )
    
        [5] => Array
            (
                [name] => Footer right box
                [value] => footer_box_right
            )
    
    )
    

    In a select tag this becomes:

    <html>
        <select>
            <?php foreach( $sidebar_options as $option ){ ?>
                <option value="<?php echo $option['value']; ?>"><?php echo $option['name']; ?></option>
            <?php } ?>
        </select>
    </html>
    

    Hope it helps!

Comments are closed.