php - How does WordPress differentiate what scripts are loaded in wp_head() and wp_footer()?

admin2025-06-03  3

A newbie question. I have gone through the wp_head() and wp_footer() documentation and both are said to be including the scripts and styles. So my question is how does WordPress differentiate which scripts need to be in the wp_head() and which needs to be in wp_footer(). My understanding is that the wp_head() will include all the scripts and styles in the header section of the webpage, and calling the wp_footer() will again include the same scripts and styles in the footer section of the website. I know it is wrong.

A newbie question. I have gone through the wp_head() and wp_footer() documentation and both are said to be including the scripts and styles. So my question is how does WordPress differentiate which scripts need to be in the wp_head() and which needs to be in wp_footer(). My understanding is that the wp_head() will include all the scripts and styles in the header section of the webpage, and calling the wp_footer() will again include the same scripts and styles in the footer section of the website. I know it is wrong.

Share Improve this question asked Feb 10, 2019 at 6:14 Denny PaulDenny Paul 1112 bronze badges 3
  • scripts are loaded whereever the wp_scripts() or wp_script() is loaded I guess, they're not part of wp_head or wp_footer, sorry I've forgotten the exact function name, I think it's wp_scripts – Aurangzeb Commented Feb 10, 2019 at 6:20
  • 1 Read through this: developer.wordpress/reference/functions/wp_enqueue_script There’s a parameter $in_footer – muka.gergely Commented Feb 10, 2019 at 6:22
  • 1 I don't understand why there is a down-vote for question. – user3864263 Commented Feb 10, 2019 at 6:30
Add a comment  | 

1 Answer 1

Reset to default 2

wp_enqueue_scripts has a parameter which specifies if the script should be rendered in wp_head or wp_footer

The function signature is this:

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

Note that it is the 5th parameter, so if you wish to not have any dependencies and use the default version number for your script you still have to supply those parameters. For example:

wp_enqueue_script( 'your_script_name', 'your_script_location', array(),false, true );
                                                                                ^     
                        //  This will cause the script to render in wp_footer   | 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748922882a314849.html

最新回复(0)