tommcfarlin / simple-autoloader-for-wordpress

An autoloader that aims to be as simple as dropping it into your WordPress project. All you need is a well-organized project.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple Autoloader for WordPress

An autoloader that aims to be as simple as dropping it into your WordPress project. All you need is a well-organized project.

More Information

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.

Getting Started

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.

  1. Clone or download this repository.
  2. Copy the lib directory into the root of your project.
  3. Add include_once 'lib/autoload.php' to your main plugin file.

An Example

This autoloader expects several things:

  1. You're following the WordPress Coding Standards as it relates to naming your classes.
  2. 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.

The Code

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

The Directory Structure

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

Adding The Autoloader

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.

Other Information

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.

Oh yeah? Watch this!

About

An autoloader that aims to be as simple as dropping it into your WordPress project. All you need is a well-organized project.

License:GNU Lesser General Public License v3.0


Languages

Language:PHP 100.0%