Title: Optimizing script loading
Author: Andrew Ozz
Published: December 27, 2008

---

 [  ](https://profiles.wordpress.org/azaozz/) [Andrew Ozz](https://profiles.wordpress.org/azaozz/)
3:12 am _on_ December 27, 2008     
Tags: [optimization ( 7 )](https://make.wordpress.org/core/tag/optimization/)

# Optimizing script loading

WordPress is using more and more JavaScriptJavaScript JavaScript or JS is an object-
oriented computer programming language commonly used to create interactive effects
within web browsers. WordPress makes extensive use of JS for a better user experience.
While PHP is executed on the server, JS executes within a user’s browser. [https://www.javascript.com](https://www.javascript.com/).
A lot of features use AJAX and nearly all UIUI User interface customizations and
enhancements depend on it. In 2.7 there is some optimization for caching, especially
when Turbo (Gears) is used all pages load in under 1 sec. However the cache control
was left to the server and there seem to be a lot of servers with less than perfect
settings for it.

There are many ways to optimize script loadingÂ in 2.8. Listing several of them 
roughly in order of ease of implementation/most effective:

 * **_Minify (but not “pack”) all js files._** Although the file size is larger,
   minified js loads faster than “packed” js, as it takes the browser some time 
   to “unpack” it. [Here’s](http://ejohn.org/blog/library-loading-speed/) a nice
   post on the subject. In 2.8 we should replace all “packed” js with minified and
   also minify all custom js when building the release.
 * **_Load most js in the footer._** Even with 15 – 20 js files a page will load
   a lot faster when most are in the footer. In theory we can load all but jQuery
   in the footer. [There’s](https://trac.wordpress.org/ticket/8628) a ticketticket
   Created for both bug reports and feature development on the bug tracker. and 
   patchpatch A special text file that describes changes to code, by identifying
   the files and lines which are added, removed, and altered. It may also be referred
   to as a **diff**. A patch can be _applied_ to a codebase for testing. for the
   script loader to do this. However in practice that will interfere with many plugins
   that load any of the default js files on adminadmin (and super admin) pages. 
   Seems that if we load jQuery and any jQuery UI parts in the head, the great majority
   of plugins won’t be affected. Another option would be to queue all UI parts for
   the footer but move them to the head if a pluginPlugin A plugin is a piece of
   software containing a group of functions that can be added to a WordPress website.
   They can extend functionality or add new features to your WordPress websites.
   WordPress plugins are written in the PHP programming language and integrate seamlessly
   with WordPress. These can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
   or can be cost-based plugin from a third-party. loaded js lists them as dependencies(
   that would complicate the script loader a bit).
 * **_Setting custom caching headers in .htaccess._** Although this may not work
   on all servers, chances are that the great majority of installations would benefit.
   There are several settings that can be added: the “expire far in the future” 
   is a must, either configuring or disabling Etags for the js, css and image files
   is advisable, also several of the HTTPHTTP HTTP is an acronym for Hyper Text 
   Transfer Protocol. HTTP is the underlying protocol used by the World Wide Web
   and this protocol defines how messages are formatted and transmitted, and what
   actions Web servers and browsers should take in response to various commands.
   1.0 cache control headers would be beneficial. Of course this would need testing
   on different production servers, but it would degrade gracefully (just wouldn’t
   work), so counting it as “must implement”.
 * **_Adding all js and css files in both compressed and uncompressed state and 
   setting the server (.htaccess) to use the compressed versions whenever possible._**
   This would greatly improve the initial loading of all scripts (cold cache), but
   is not as trivial to set up as the cache control headers. Still worth a try even
   if not all WordPress installations would benefit. It is possible to set the server
   to compress “on the fly” or compress by using php, but both methods use a lot
   more of server CPU time/memory on each page load. Since this affects only cold
   cache hits, not sure if it’s worth it. The typical user case for the admin is
   the same user on the same computer and in the same browser accessing the admin
   at least several times per week. So making sure caching works best is a priority.
 * **_Concatenating all default js files on per page basis so there’s only one js
   file to load._** This combined with compressing the file would give the fastest
   loading time for both cold and hot cache, although for hot cache with proper 
   cache control the difference is almost unnoticeable. It is possible to do this
   when building the release and include two versions of each file: compressed and
   uncompressed, then use .htaccess to output the compressed version when possible.
   However when there is a plugin that loads js and depends on other default js 
   files, the performance degrades quickly and most blogs seem to have at least 
   one such plugin. The workaround is to dynamically generate the concatenated file
   for each page, compress it and store it on the server. That would mean doing 
   something similar to WP Super Cache but only for the js and css files in the 
   admin. There seem to be at least one plugin that attempts to do this, also several
   regular WordPress contributors are interested in making such plugins. Perhaps
   for now it’s better to leave this for a plugin as there seem to be many server
   configurations where it would be difficult to set up or won’t work properly. 
   We can also add some hooksHooks In WordPress theme and development, hooks are
   functions that can be applied to an action or a Filter in WordPress. Actions 
   are functions performed when a certain event occurs in WordPress. Filters allow
   you to modify certain functions. Arguments used to hook both filters and actions
   look the same. in script loader to facilitate this if needed.

In terms of performance the concatenating and compressing of all js and css files
for each page gives the best results, closely followed by the loading of most js
files in the footer. If we decide not to do the concatenating/compressing, we should
revisit it in about a year (for WordPress 3.0?) as most Web hosts would have upgraded
to php 5 by then. The actual php version wouldn’t affect this, but it seems that
while upgrading the hosts also improve their server and php configurations too.

[#optimization](https://make.wordpress.org/core/tag/optimization/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2008/12/27/optimizing-script-loading/?share=linkedin)

 1.  ![](https://secure.gravatar.com/avatar/f8e8683cd1462a81fad9564baa68e6beb1ff73d1d41bde645a225ff07e5d5093?
     s=32&d=mm&r=g)
 2.   Gregory  3:57 am _on_ December 29, 2008
 3.  It would be great if wordpress was compatable with mod_concat and mod_jsmin. I
     have attempted to implement both apacheApache Apache is the most widely used web
     server software. Developed and maintained by [Apache Software Foundation](https://www.apache.org/).
     Apache is an Open Source software available for free. modules with 2.7 with limited
     results
 4.  ![](https://secure.gravatar.com/avatar/c28532baf902919df47ab6d8aff5398bfb166a196ffbdebcffe7960293e78024?
     s=32&d=mm&r=g)
 5.   Jacob Santos  3:58 pm _on_ December 29, 2008
 6.  If hosting servers don’t support PHP5 now, then they’ll probably won’t in another
     year or two.
 7.  ![](https://secure.gravatar.com/avatar/7d64fa1704b7166636e200f2bb0d603d26d144b07f5993c18f3757f578886225?
     s=32&d=mm&r=g)
 8.   [Rahul Boss](http://www.sizhot.com)  10:44 am _on_ December 30, 2008
 9.  mainly now all hosting servers support php5.
 10. ![](https://secure.gravatar.com/avatar/9ba420a97060b6055c7e89d8cce09137ce4712f24c6bc27b153d81a5bb18cec3?
     s=32&d=mm&r=g)
 11.  [miloandrew](http://miloandrew.wordpress.com/)  11:03 pm _on_ December 31, 2008
 12. Many of your ruminations are understandably taking into account the huge array
     of available WP plugins. While this is absolutely necessary, maybe taking a bit
     of a different tack can open up some options?
 13. I have often wished that I could manage the load order of plugins and control 
     which loads before another, or even WHEN a pluginPlugin A plugin is a piece of
     software containing a group of functions that can be added to a WordPress website.
     They can extend functionality or add new features to your WordPress websites. 
     WordPress plugins are written in the PHP programming language and integrate seamlessly
     with WordPress. These can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
     or can be cost-based plugin from a third-party. loads at all. Could it be that
     implementing more internal plugin declaration and control could help with the 
     overall compatibility of this optimization initiative?
 14. If we make it possible for the system to detect the way certain plugin scenarios
     will affect optimization efforts, then the system can adapt on its own. Rather
     than making global decisions on optimization based upon what COULD exist with 
     certain plugins, maybe taking the tack of allowing the plugins to tell the system
     what they load and require would do the trick?
 15. The plugin headerHeader The header of your site is typically the first thing people
     will experience. The masthead or header art located across the top of your page
     is part of the look and feel of your website. It can influence a visitor’s opinion
     about your content and you/ your organization’s brand. It may also look different
     on different screen sizes. could potentially be extended to either allow the developer
     to state when the plugin should be loaded, or at least to specify other items 
     that it either requires or loads itself. A “Loads:” field along with a “Requires:”
     field could go a long way to helping WP understand what the activated plugins 
     require. It could also help the user make intelligent decisions by allowing the
     system to report if the activation of a specific plugin will necessitate the disabling
     of a specific optimization technique that would otherwise be used by the coreCore
     Core is the set of software required to run WordPress. The Core Development Team
     builds WordPress..
 16. Alternatively (or in addition to the above), you could allow the WP adminadmin(
     and super admin) to specify where and when all active plugins are loaded. While
     I would love to see this type of completely configurable plugin management, I 
     think that even just the additional declarations could be extremely helpful.
 17. Sorry – I don’t mean to digress from optimization to plugin management, but it
     seems that there may be a few strong ties between the two.
 18. Thanks for listening!
 19. Cheers,
      A
 20. Pingback: [Blog Herald WordPress News: WordPress Scholarship, 800,000 WordPress 2.7 Downloads, XML-RPC Problem, and Top Plugin Authors « Lorelle on WordPress](http://lorelle.wordpress.com/2009/01/02/blog-herald-wordpress-news-wordpress-scholarship-800000-wordpress-27-downloads-xml-rpc-problem-and-top-plugin-authors/)
 21. ![](https://secure.gravatar.com/avatar/40432e83f7cab75f4fff5f7d51043a678713f8072c4343b0cb0f9d2baff1c47d?
     s=32&d=mm&r=g)
 22.  [Andrew Ozz](http://azaozz.wordpress.com/)  10:22 am _on_ January 3, 2009
 23. @Jacob Santos, @Rahul Boss yes, but most hosts require the user to manually switch
     to php 5, sometimes by directly editing .htaccess. Don’t think that most users
     are comfortable doing that or even aware of it.
 24. @miloandrew almost all of these features already exist in the APIAPI An API or
     Application Programming Interface is a software intermediary that allows programs
     to interact with each other and share data in limited, clearly defined ways.. 
     In the case of script loading, plugins can enqueue both js and css and require
     any of the scripts included in WordPress. Unfortunately not all plugins use the
     script loader, but most do.
 25. The optimization will have to be transparent for the plugins. For example setting
     appropriate caching headers for js, css and images in .htaccess would affect all
     scripts.
 26. The load order can be managed from the filterFilter Filters are one of the two
     types of Hooks [https://codex.wordpress.org/Plugin_API/Hooks](https://codex.wordpress.org/Plugin_API/Hooks).
     They provide a way for functions to modify data of other functions. They are the
     counterpart to Actions. Unlike Actions, filters are meant to work in an isolated
     manner, and should never have side effects such as affecting global variables 
     and output./action priority quite well. The blogblog (versus network, site) adminadmin(
     and super admin) can edit the pluginPlugin A plugin is a piece of software containing
     a group of functions that can be added to a WordPress website. They can extend
     functionality or add new features to your WordPress websites. WordPress plugins
     are written in the PHP programming language and integrate seamlessly with WordPress.
     These can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
     or can be cost-based plugin from a third-party.’s file and change that if needed
     or have a small plugin that would set it up for the rest.
 27. BTW the first step is already implemented. All js has been minified reducing the
     size by about 30% – 35%.
 28. Pingback: [WordPress News: WordPress Resolutions, Tattoos, LiveJournal, Converters, Security, Plugins, and More « Lorelle on WordPress](http://lorelle.wordpress.com/?p=3589)
 29. Pingback: [WordPress News: WordPress.tv, WordCamp Whistler, WordPress Logo, City Saves Money, and More | The Blog Herald](http://www.blogherald.com/2009/01/23/wordpress-news-wordpresstv-wordcamp-whistler-wordpress-logo-city-saves-money-and-more/)
 30. Pingback: [WordPress News Report: iPhone, Widgets API, WordCamps in Hong Kong and China, International WordPress.tv « Lorelle on WordPress](http://lorelle.wordpress.com/2009/03/19/wordpress-news-report-iphone-widgets-api-wordcamps-in-hong-kong-and-china-international-wordpresstv/)
 31. Pingback: [Wordpress 2.8 para Abril de 2009 | aNieto2K](http://www.anieto2k.com/2009/03/19/wordpress-28-para-abril-de-2009/)
 32. Pingback: [WordPress 2.8 previsto para abril » Comunidade WordPress-BR](http://www.wordpress-br.com/novidades/wordpress-28-previsto-para-abril)
 33. ![](https://secure.gravatar.com/avatar/ea098712583c160c0f16a813f0455179f08d61e222303221f13bcb7eea9a15ee?
     s=32&d=mm&r=g)
 34.  [Milan](http://milandinic.com/)  1:31 pm _on_ March 22, 2009
 35. I am not sure where to post this, but I’ll start from here since title of this
     post describes my intention.
 36. When new comment threading for 2.7 was introduced, you [recommended](http://ottodestruct.com/blog/2008/09/29/wordpress-27-comments-enhancements/)
     call to comment-reply.js in [this form](https://codex.wordpress.org/Migrating_Plugins_and_Themes_to_2.7/Enhanced_Comment_Display#Javascript_Comment_Functionality).
 37. But problem with this is that comment-reply.js is called on all pages in all cases
     even there are many cases when it shouldn’t be called because it is simply not
     needed:
 38.  * when comments are turned off on site wide level
      * when threaded comments are not enabled (this is default option)
      * when comments are not enabled on that entry
      * when there is no comments on that entry
      * when page is used as front page (in reading options)
 39. In all those situations comment-reply.js is loaded even though there is no need
     for it. Yes, it is cached after first page view, but what if visitor lands on 
     that particular page that doesn’t need this file and that page already has ten
     javascriptJavaScript JavaScript or JS is an object-oriented computer programming
     language commonly used to create interactive effects within web browsers. WordPress
     makes extensive use of JS for a better user experience. While PHP is executed 
     on the server, JS executes within a user’s browser.
      [https://www.javascript.com](https://www.javascript.com/)
     files? It will just increase page load time and you know that visitors that come
     from search engines or from links on other sites have low tolerance and don’t 
     want to wait to much.
 40. So what we need is that this script is loaded only when there is need for it and
     my suggestion is that you make some function that should be checked and that will
     return true or false value when asked if there is need for comment-reply.js. Maybe
     that script can’t solve all problems but at least it can return false value if
     comments are globally turned off or comment threading is not enabled (I’m quite
     sure it can do this).
 41. Example of how this should work in theme’s files:
 42. `if ( comment_threading_entry_status() ) wp_enqueue_script( 'comment-reply' );
 43. Example of functions:
      `function comment_threading_entry_status() { if (((get_option('
     default_comment_status')) == "open") && ((get_option('thread_comments')) == "1")&&(!
     is_front_page() ) && ( comments_open() ) && ( have_comments() )) { return true;}
     else return false; }
 44. This function doesn’t work like this (it works without last two conditions), but
     you got a point.
 45. I hope that you’ll at least respond me in reasonable time 🙂
 46. (btw, why we don’t have subscribe to comments here, it should be enabled on wp.
     com?)
 47. Pingback: [WordPress 2.8 kommt im April 2008 - Template, Nutzung, Scripte, WordPress, Sicht, SimplePie - WordPress Deutschland Blog](http://blog.wordpress-deutschland.org/2009/03/23/wordpress-28-kommt-im-april-2008.html)
 48. Pingback: [WordPress 2.8, small insights - SimplePie, WordPress, Template, More, Core, With - WP Engineer](http://wpengineer.com/wordpress-28-small-insights/)
 49. Pingback: [Tobbis Blog » WordPress 2.8 kommt im April](http://www.tobbis-blog.de/internet/wordpress/2009-03-26-wordpress-28-kommt-im-april/)
 50. Pingback: [Preview do WordPress 2 .8 | ivanBogar.com | design e wordPress](http://www.ivanbogar.com/2009/preview-do-wordpress-2-8/)
 51. Pingback: [WordPress News: WordPress 2.8, Summer of Code, PodPress Returning, iPhone Update | The Blog Herald](http://www.blogherald.com/2009/03/27/wordpress-news-wordpress-28-summer-of-code-podpress-returning-iphone-update/)
 52. ![](https://secure.gravatar.com/avatar/8c626a62e0b1a18ff6524d1e91cedecba844ff6f635bef5e4d4126f8aa921278?
     s=32&d=mm&r=g)
 53.  [John Biddle](http://www.poolstudent.com/)  8:39 pm _on_ April 2, 2009
 54. This is a great idea. The problem is even bigger than you describe. It’s true 
     that for second and subsequent pageloads the javascriptJavaScript JavaScript or
     JS is an object-oriented computer programming language commonly used to create
     interactive effects within web browsers. WordPress makes extensive use of JS for
     a better user experience. While PHP is executed on the server, JS executes within
     a user’s browser.
      [https://www.javascript.com](https://www.javascript.com/) is
     is the cache, it’s also true that unless the files has an expired headerHeader
     The header of your site is typically the first thing people will experience. The
     masthead or header art located across the top of your page is part of the look
     and feel of your website. It can influence a visitor’s opinion about your content
     and you/ your organization’s brand. It may also look different on different screen
     sizes. the browser still has to contact the host to determine if the file in cache
     is current. There won’t be a download for current files, but the round trip to
     the server just to check the currentness wastes time for every page. This problem
     exists for losts of files in lots of plugins and probably contributes to a 1 or
     2 second page delay for the average page.
 55. Pingback: [WordPress Plugins News: Coffee2Code Plugin Marathon, Plugin Podcast, Plugin Developer Center, Plugin Checklist « Lorelle on WordPress](http://lorelle.wordpress.com/2009/04/05/wordpress-plugins-news-coffee2code-plugin-marathon-plugin-podcast-plugin-developer-center-plugin-checklist/)

Comments are closed.

# Post navigation

[← Fixing bugs for 2.7.1 and thinking about …](https://make.wordpress.org/core/2008/12/12/fixing-bugs-for-271-and-thinking-about/)

[Optimizing script loading, part II →](https://make.wordpress.org/core/2009/01/07/optimizing-script-loading-part-2/)