WPAL – WordPress AutoLoader Plugin Released
Want to write better PHP code for your WordPress themes and plugins? Of course you do. One way to do that, is to use an autoloader. An autoloader lets you dynamically require or include a file based on it’s class name. As developers, we strive to make projects more manageable. We organize our code into multiple files, and name them well. When using object-oriented programming, autoloading classes makes it easier to manage your code. That’s the idea behind the WPAL project.
WordPress Needs An Autoloader
WordPress doesn’t currently have an autoloader. The best autoloader PHP currently has available comes from Composer. Composer supports PHP versions all the way back to 5.3.x. However, even given this liberal constraint, WordPress Core can’t use it due to supporting PHP 5.2.x … because reasons. That’s a different conversation and beyond the scope of this article.
However, that’s not the only constraint. WordPress names its class files differently than other PHP projects. So without WordPress changing it’s filename standard for classes, it really needs to create its own autoloader. Also, after reading the WordPress PHP Coding standard, I did not see a standard for dealing with interfaces and traits.
Because of these constraints, I created WordPress AutoLoader plugin.
The Benefits of AutoLoading
Since WordPress Core doesn’t provide an autoloader, core files are statically loaded during the WordPress loading process. There are a couple of benefits to WordPress adding an autoloader – even if it isn’t based upon this code.
- WordPress could restructure how it organizes its files while maintaining functionality
- Plugin and Theme authors could use it to manage their own classes.
One of the great things about WordPress is it has a culture of creating a feature plugin first. This prevents the core of WordPress from becoming bloated. If it’s useful to enough people, then the functionality can be added to core later. With that in mind, I wanted to create a WordPress autoloader as a plugin. Unfortunately, it can’t be added to the wordpress.org plugin repository, because it’s seen as a library – which is a class of plugin they don’t want to allow. So it’s available on my github.
Learn more about it by reading about the created WordPress AutoLoader pluginĀ project. I’d like to get some feedback on it, so I can improve it.