0

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.

migrated from stackoverflow.com Feb 10 at 8:30

This question came from our site for professional and enthusiast programmers.

  • 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 Feb 10 at 6:20
  • 1
    Read through this: developer.wordpress.org/reference/functions/wp_enqueue_script There’s a parameter $in_footer – muka.gergely Feb 10 at 6:22
  • 1
    I don't understand why there is a down-vote for question. – user3864263 Feb 10 at 6:30
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   | 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.