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 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.