How to List all Sidebars in a Metabox

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!

Leave a Comment