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
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 🙂
Try this:
This prints out( with my registered sidebars ):
In a select tag this becomes:
Hope it helps!