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 trialJAKZ AIZZAT
7,813 PointsWhat'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
28,149 PointsFirst 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.
Zac Gordon
Treehouse Guest TeacherZac Gordon
Treehouse Guest TeacherGreat answer Kevin!
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsThank you Zac. Learned most of my WP knowledge from your lessons.