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

How does wp_head() know what to load?

Hey,

So I'm doing the Wordpress Theme Development course and I've got a question. How does wp_head() work in the header.php file? I'm just not sure I understand it.

I guess the problem I'm having is that in the functions.php file, I have 2 different functions, one that enqueues the script files, and one that enqueues the stylesheets. Then, there are 2 add_action in the functions.php file that tell wp when it's time to load the scripts, call one function, then the other, to load all the script and stylesheets. But how does it know where to load?

I remember the script files had a true or false. False, as I understand it means, load in the header, and true, as I understand it, means, load in the footer. But how do the stylesheets know where to load? Do they just automatically load in the header with wp_enqueue_script()?

Thanks.

Victorino Machava
Victorino Machava
4,387 Points

Hi Robin,

I'd like to add that stylesheets are usually loaded in the header since it's usually related to how the page looks, hence the need to load it before loading the rest of the page's elements.

1 Answer

Robin,

"wp_head" is a "hook" function to pull in the css and js links from the theme file (via wp_enqueue_scripts) on top of pulling in other meta data, etc. with other functions. The "wp_head" function doesn't do anything aside from running a bunch of other functions nested within it. All of the functions associated with "wp_head" are listed within "wp-includes/default-filters.php".

It's a little complicated because everything is nested together.

I hope this helps!

Hi Mark,

Thanks for your response. I guess I'm just not sure what is connected to the wp_head() and what is connected to the wp_footer(). Does it say what is hooked to the wp_head() and wp_footer() inside the default-filters.php file?

Hi Mark.
Your comment really clarified for me how wp_head() works.
And just to clarify, in the case of the example from this video, wp_head() is essentially calling in wp_enqueue_scripts twice, once re the stylesheets and once re the scripts; b/c of how it's coded it knows that the stylesheets should be placed where the wp_head() hook is located, and b/c of the true/false parameter in wp_enqueue_script it knows where to put the scripts, either in the header (false) or footer (true).
Is that correct?
Where i get confused now is that since wp_head() is located in header.php, how does it include scripts in the footer if the footer is actually in another file (footer.php)? The only way this would make sense to me is if index.php "inserts/requires" get_header() and get_footer() as the first step, then WP parses index.php with the header code in it (which subsequently includes wp_head() and now since there is a footer in the document it looks for wp_footer and inserts any "true" scripts there)...
Is that correct?

How do I find all the functions associated w/ wp_footer() BTW? wp-includes/default-filters.php only lists one?