How do register_sidebar() and get_sidebar() work together?

Disclaimer: I am brand spanking new to WP.

I am using the Starkers HTML5 framework theme. In the functions.php I see this code:

function starkers_widgets_init() {

  // Area 1, located at the top of the sidebar.
  register_sidebar( array(
    'name' => __( 'Primary Widget Area', 'starkers' ),
    'id' => 'primary-widget-area',
    'description' => __( 'The primary widget area', 'starkers' ),
    'before_widget' => '<li>',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  ) );

  // Area 3, located in the footer. Empty by default.
  register_sidebar( array(
    'name' => __( 'First Footer Widget Area', 'starkers' ),
    'id' => 'first-footer-widget-area',
    'description' => __( 'The first footer widget area', 'starkers' ),
    'before_widget' => '<li>',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
  ) );

  // ... more calls to register_sidebar() ... 
}

And in footer.php I see this code:

<?php get_sidebar( 'footer' ); ?>

I don’t understand how get_sidebar() knows how to take that string argument and find the appropriate widgets that were defined by register_sidebar(). In the functions.php snippet I posted above. There isn’t any mention of “footer” except for the name, id and description properties. But it would seem odd to me that get_sidebar() would search for ‘footer’ inside those properties.

Does this make sense what I am asking? Is there some missing piece?

The reasons I am asking is because
– I would like to know more about the WP architecture
– I would like to be able to define a custom widget area and know how to render it on a specific page.

Thanks a ton.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You just call get_sidebar() from index.php and it loads the theme file sidebar.php.

register_sidebar(), on the other hand, is used for widgets where plugins and such want to dynamically add content in your sidebar.php file if your theme supports it.

In your case, is there a file called sidebar-footer.php in your theme’s directory?

Method 2

I’ve never bothered with get_sidebar(). Instead I just use dynamic_sidebar(). You’d call it like this:

dynamic_sidebar('first-footer-widget-area');

And that takes care of the whole sidebar. No more file inclusions, no more cluttered theme folders. If I want to have 5 different sidebars, it doesn’t add any files, only extra functions in functions.php.

Method 3

get_sidebar('name') gets a sidebar template of the name sidebar-name.php
.

Within sidebar-name.php, there is the HTML for the sidebar, and a call to dynamic_sidebar('some-name-hopefully-the-same'), which is where the widgets will go.

register_sidebar(array(name=>'some-name-hopefully-the-same', ...)) is what allows dynamic_sidebar to work.

As you can see, get_sidebar(templatename) is for use with sidebar-templates. If you have no need for them, you can just call dynamic_sidebar(sidebarname) directly from your theme.

Method 4

get_sidebar('footer') attempts to load sidebar-footer.php from the active theme. Starkers does provide this file. Check starkers/sidebar-footer.php and things should become clear.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x