简体   繁体   中英

When enqueuing javascript in wordpress, wp_enqueue_scripts vs wp_footer

Is there any disadvantage to using wp_footer as the hook when enqueuing javascript files in a wordpress theme? I have about 5 files that I want to be enqueued in the footer but setting the wp_enqueue_script property for footer to true isn't doing anything.

I have wp_footer() in my footer file and when I change the action to:

add_action('wp_footer', 'extra_scripts');

the scripts load fine in the footer. This is just for a demo and not anything more than just a front end display. Is there anything wrong with doing it this way to load the scripts in the footer?

You should enque what you can in the footer. To force this use:

// force all JS to the footer
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);

This way you can use the default wp_enqueue_scripts
Do remember that some scritps need to be in the <head> like the HTML5 script of twentyeleven/twentyten

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM