How to Get Registered Sidebars WordPress with Foreach loop

get registered sidebars WordPressDo you want to see a practical example of how to use a loop to get registered sidebars in WordPress? If that is what you are looking for this is the best tutorial for you. I will demonstrate with a practical example on how to get registered sidebars in WordPress and then work on your script logic.

Why Get Registered Sidebars in WordPress?

There are a variety of reasons or logical scenarios where you may want to get all the registered sidebars in WordPress. A case example is when you want to show the sidebars based on conditional state.

Like when you may want to display the sidebars only when a certain box is checked. In such a situation, you may need to get all the sidebars and sort them in your logical sequence.

But first, before you can get to manipulate the registered sidebars in WordPress, you need to know how they are registered. I did explain in detail before here – on how to register a sidebar in WordPress with a vivid example.

How to Register a Sidebar in WordPress

When you are creating WordPress themes, you need to know how to register WordPress sidebar or how to add a sidebar in WordPress theme.

When you have successfully added the WordPress sidebar in your theme, you can now move to the advanced topic s like how to remove the sidebar in WordPress and now how to get registered sidebars in WordPress.

Two Steps to Get All the Registered Sidebars WordPress

To get registered sidebar in WordPress theme we essentially need to get the data from an array and then loop through the data to find what we want. In the following steps you can get registered Sidebars in WordPress:

  1. Get the global object for all the registered sidebars that contain this information
  2. Use a foreach loop to iterate through the array of the global sidebars object and now you can output the registered sidebars in WordPress theme.

Let me illustrate these steps with an example; first, we need to create a function and use the global $wp_registered_sidebars  to obtain all the sidebars that are registered in WordPress.

This is the special global object that contains all the registered sidebars. In a function we can have it as follows:

/**
 *  Get all registered sidebars in WordPress
 *  @link https://gist.github.com/Njengah/c5360418680cd89dbb92b3c77bcc527c
 */

function get_all_registered_sidebars_in_wordpress(){             

         //global sidebars object
                                
          global $wp_registered_sidebars;

        //test the contents of the global object with print_r and die() for better display

        print_r($wp_registered_sidebars);

         //use die() so that we view the data alone

       die();

}
// admin_init hook to display the data on admin side 

add_action('admin_init' , 'get_all_registered_sidebars_in_wordpress'); 

 

We can now use the foreach loop to now manipulate the data to do whatever we need.

Example the function can be changed to this way to display a list of all the registered sidebars in your WordPress theme:

function  get_all_registered_sidebars_in_wordpress(){

     //global sidebars object

     global $wp_registered_sidebars;

      //loop through the sidebar object and display the names of registered sidebars

      foreach ( $wp_registered_sidebars as $sidebar ) {

      if (!empty( $sidebar['name'] ) ) {

               echo $sidebar['name'] . "<br>";

         }

      }      

       die();    

}

How to Get Registered Sidebars WordPress

Wrapping Up

You can get registered sidebars in WordPress using the global $wp_registered_sidebars   and you can now manipulate the data to suit your logic. I hope this quick tutorial was elaborate and you can easily implement this solution in your WordPress plugin or theme development.