[wp-hackers] Optimize wp_register_script and wp_enqueue_script functions

zhaiziming at gmail.com zhaiziming at gmail.com
Wed Jan 25 23:24:36 UTC 2012

Hey all,

As I know, most of the plugins use wp_register_script() and 
wp_enqueue_script() to add js files into frontend pages. The current 
functionality will affect page load speed if you use many plugins. The 
problem is if you enqueue a js file, it will add this file to all the 
frontend pages, no matter if it is been used in these pages or not.
For example, I add a plugin to vote categories which has two script 
files. They should be only been added in the category page, but turn out 
wordpress adds to all the pages like index, post and page. It is not 
good for site performance.
My current solution is to write a function to deregister all the scripts 
and add them back on different pages, and then hook this function to 
init action.

It would be great if wp_register_script and wp_enqueue_script have the 
feature to target the script to specific pages. Ultimately, it would be 
even great if wordpress support javascript lazy load (on demand). I am 
sure we will get a lot of performance benefits from it.

Any suggestions and comments?

zhaiziming at gmail.com

More information about the wp-hackers mailing list