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


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.





Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.