Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Make a Website with WordPress WordPress Widgets and Custom Menus How to Create Widgetized Areas in WordPress

Daniel Schroeder
Daniel Schroeder
13,859 Points

How does the %1$s and %2$s portion of create_widget() work?

I notice that this line from create_widget():

'before_widget' => '<div id="'.$id.'" class="widget %1$s %2$s">'

turns into this in the html that is served:

<div id="uptop" class="widget text-3 widget_text">

How does %1$s eventually turn into text-3 and %2$s turn into widget_text? I understand that those are related to sprintf, but that function is not being called here, correct? I'm sure it must have something to do with the internal WordPress functions, but I can't find any documentation that says how this works and browsing through the code in widgets.php has still left me clueless.

Here is the complete function for reference:

// Function for creating Widegets
function create_widget($name, $id, $description) {

  register_sidebar(array(
    'name' => __( $name ),    
    'id' => $id,
    'description' => __( $description ),
    'before_widget' => '<div id="'.$id.'" class="widget %1$s %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>'
  ));

}

// Create the actual widgets
create_widget("Header", "uptop", "Displays in the header of the site, above the title");