An autoloader that aims to be as simple as dropping it into your WordPress project. All you need is a well-organized project.
TL;DR: An autoloader you can drop into a WordPress plugin and begin using it automagically.
In 2017, I gave a talk at WordCamp Atlanta about the importance of using Namespaces and Autoloading in WordPress.
Though for many projects, we can't adopt many of the new features of PHP7+, that doesn't mean can't use best practices when working on plugins and other projects.
I have a very simple autoloader that I'm sharing in this repository that I hope the greater (and smarter!) WordPress developers at large will contribute to improving.
This particular section is for those who want to use the autoloader. If you're looking to contribute to the codebase, please see the section below.
- Clone or download this repository.
- Copy the
lib
directory into the root of your project. - Add
include_once 'lib/autoload.php'
to your main plugin file.
This autoloader expects several things:
- You're following the WordPress Coding Standards as it relates to naming your classes.
- The structure of your namespaces follows the structure of your directory structure
I've provided an example below for how both your code and your directory should be organized to take advantage of the autoloader.
Let's say you have a plugin and one of the files contains the following namespace:
namespace Pressware\API;
And it's using a class in another namespace:
use Pressware\Utility\Files\Reader;
The autoloader expects that the root namespace defined in your main plugin file to be:
namespace Pressware
And that all of the rest of the files are located in a directory structure like this:
+ plugin-name
|
| API
| ...
| ...
|
| Utility
| ...
|
| Files
| class-reader.php
| ...
|
| plugin-bootstrap.php
Then, at the top of your plugin file add the following:
require_once 'lib/autoload.php';
This can work alongside another other autoloaders (such as those that come with Composer) and will prevent you from
needing to add require_once
or include_once
all over the state of your application.
If you're interested in contributing, reading more, and or following changes (all of which is welcome), please read below.
- The project is licensed GPL.
- If you're interested in contributing, please read this document.
- See the CHANGELOG for a complete list of changes.