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

JAKZ AIZZAT
JAKZ AIZZAT
7,813 Points

What's meaning of this widget %1$s %2$s ?

Whats meaning of widget %1$s %2$s in 'before_widget' => '<div id="'.$id.'" class="widget %1$s %2$s">',

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

First things first, the Codex tells us "HTML to place before every widget(default: '<li id="%1$s" class="widget %2$s">') Note: uses sprintf for variable substitution"

So we need to know what sprintf does. The PHP docs http://www.php.net/manual/en/function.sprintf.php show us it it's basically a variable placeholder. So where is wordpress actually substituting %1$s and %2$s?

Well we have to dig way down into the WP core files https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/widgets.php#L0 all the way down to line 1016 we see this line

$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

This basically is going through every registered widget and pulling out the id and the classname from the arguments and using the variable substitution to put them into the HTML if those arguments exists.

That's way oversimplified explanation, but it essentially what's happening.

Kevin Korte
Kevin Korte
28,149 Points

Thank you Zac. Learned most of my WP knowledge from your lessons.