[wp-hackers] Optimize wp_register_script and wp_enqueue_script functions

Jackson Whelan jw at jacksonwhelan.com
Wed Jan 25 23:30:02 UTC 2012


On 1/25/12 6:24 PM, zhaiziming at gmail.com wrote:
> 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?
Here's an excellent article from Scribu:

http://scribu.net/wordpress/optimal-script-loading.html

- Jackson


More information about the wp-hackers mailing list