Files Loaded Automatically as Needed

This is an advanced method that requires classes to use. Instead of requiring files manually they are required automatically whenever a given class is invoked. Because it does require classes, many developers never use or even learn about this method of loading files but it can have significant gains with minimal effort, especially if code is already abstracted correctly and using an OOP approach.

There are several ways to setup the autoloader. The basic approach used here is to compare to an array using in_array. Other methods include having a library or even using file_exists to check if the file needed is in the folder.

One limit is that files need to be abstracted and named correctly. The class has to be part of the file name and in general the file should contain only the one class.

More complex plugins may have the class files broken into multiple folders. This method can still work but may require more than one library or methods based on the class name (for example include admin in the class names in the admin folder) to require files from the correct folder.


  • Class based files are automatically loaded so you don't have to check if a class exists before loading it
  • Code is extremely simple
  • Huge gains in efficiency and prepares for even bigger gains
  • Encourages properly abstracted code which can improve testability and keep code more streamlined


  • Since this is not yet using the advanced hooks the code still has high potential for conflict. This is especially true with JS and CSS because those files are always being loaded

Test Notes

Compared to the 0.1 Least Efficient and 0.1 is_admin the number of HTML comments generated by the WCDC_Advanced_Hooks objects are reduced even more. On a given page load in the dashboard you will see a reduction to 5,000 HTML comments, nearly half. The front end gets an another 1000 fewer to 4000 HTML comments, which is over half.

While this does mean any given plugin would see some very big gains, it should be noted that there is still a lot of room for improvement, especially considering the css and js is still being loaded everywhere.

At this point we can start adding a lot of conditional statements to control loading code. This works and can solve the code conflict potential, but using advanced hooks instead reduces the need for complex conditional code which can break and be difficult to diagnose. The next step will see the biggest improvements and solve the conflict issues, at least as much as we can from our plugin.