How to list all sidebars in WordPress?

Sidebar is a theme feature introduced with Version 2.2. It’s basically a vertical column provided by a theme for displaying information other than the main content of the web page. Themes usually provide at least one sidebar at the left or right of the content. Sidebars usually contain widgets that an administrator of the site can customize. Often, we are in situation to allow the users to select correct sidebars for the posts or page or any custom type posts. So, here is a quick article showing how we can list all the selected sidebars.

Here, $GLOBALS[‘wp_registered_sidebars’] will return an array of sidebars name. Then, all we need to do is parse the sidebars array and echo them as required.

Lists all registered sidebars

<select>
<?php foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) { ?>
     <option value="<?php echo ucwords( $sidebar['id'] ); ?>">
              <?php echo ucwords( $sidebar['name'] ); ?>
     </option>
<?php } ?>
</select>

This could be handy creating post of theme options page for increasing usability of the theme. For more details about sidebars read codex sidebar documentation.

About the Author

dr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.