It’s not hard to find criticism about namespacing and autoloading in WordPress, and lack thereof. As much as I’d like to see it, I think it’s important to take a practical look at the software as a whole, the requirements, and realize that implementing such organization would require a lot.
Specifically, it would require applying object-oriented programming throughout the entire codebase (which would last longer than a release cycle in and of itself).
It would also require that all hosts who support WordPress on any level (yes, even those supporting legacy versions) can handle the new features.
In short, it’s not an easy task and it’s important to recognize the practical challenges that come with doing so in 13 year old software powering approximately 25% of the web.
It’s not that it can’t be done, that it won’t be done, that people don’t want it done. But it’s requires exceptional planning, execution, testing, and support from a wide array of situations that I don’t know if I can even fully grasp.
With all of that said, though, this doesn’t mean that we can’t use namespaces and autoloading in our WordPress projects.
Namespacing and Autoloading in WordPress
In my latest series for Envato, I’m talking about how to implement this type of code within the context of a custom plugin.
From the series’ page:
In this series, we’re going to take a look at exactly what PHP namespaces are, why they are beneficial, and how to use them. Then we’re going to take a look at how to use autoloaders to automatically load the files that we need without having to manually load them in our code.
Each tutorial walks through the process of creating a plugin that uses namespaces and creating an autoloader based on the organizational scheme.
The goal is not to argue a case for why it should or shouldn’t be in WordPress. Instead, it’s to show how – given the proper setup – we’re able to create plugins that use these features of PHP.
The series assumes you have no prior knowledge of namespaces or autoloaders and starts from the very beginning.
So if you’re curious about how to apply namespacing and autoloading within the context of WordPress, then checkout the tutorials.
To Theme Developers
Note, however, that if you’re a theme developer this content is likely not for you. Because of the way themes are built and designed, it’s not the most applicable content.
It’s primarily geared towards plugin developers and those who are building object-oriented solutions on WordPress.