Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,105 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,105 Points

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