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 WordPress Theme Development WordPress Header and Footer Templates Porting existing headers and footers into WordPress

caroline
caroline
6,974 Points

How does WordPress 'know' which hook to use?

I don't know if I'm missing something obvious here, but I'm wondering how add_action ( 'wp_enqueue_scripts', 'wpt_theme_styles'); knows to use the wp_head() hook so that the stylesheet URLs are outputted to header.php. Similarly, how does add_action( 'wp_enqueue_scripts', 'wpt_theme_js' ); know to use the wp_footer() hook so that the js is loaded into the footer?

Clearly WordPress 'knows' that this should be the case, but I can't understand how. I couldn't find anything obvious in the codex. Can anyone help? Thanks!

caroline
caroline
6,974 Points

I've worked it out now - wp_enqueue_style is automatically added to header.php (so uses wp_head() ), and wp_enqueue_script will either load in the header or footer depending on the last parameter and whether it is set to true or false - I remember Zac covered this in an earlier video.

More here https://developer.wordpress.org/themes/basics/including-css-javascript/

I'll leave this here in case it helps anyone else.