Class, Trait and Interface Autoloader for WordPress
Report Bug or Suggest Improvement
Table of Contents
The WordPress-Autoloader is an autoload class for your WordPress theme or plugin.
It implements the process of automatically loading PHP classes, traits and interfaces without explicitly loading them with control structures like require(), require_once(), include() or include_once().
WP_Autoloader class constructor accepts one optional parameter which is relative path to application folder where files located.
The project also contains composer.json and phpcs.xml.dist configuration files for PHP/WordPress code sniffers and WordPress translation tool.
Please remember that in order to successfully use this autoload solution it's necessary to name your class, trait and interface files strictly according to the naming convention.
Make sure you have installed:
- PHP 7.4+
- Composer
-
Clone or download the repo into root of your WordPress theme or plugin folder:
git clone https://github.com/alexander-kozhukhovski/wordpress-autoloader.git
-
Remove
.gitfolder and.gitignorefile (assume that you already have configured Git in your project):rm -r .git rm .gitignore
-
Complete following find-and-replace actions in
phpcs.xml.dist,composer.jsonandclass-wp-autoloader.phpfiles:- Search for
my-appto capture the text domain and.potfile and replace it with your values - Search for
My_Appto capture initial namespace and DocBlocks and replace it with your values
- Search for
-
Run composer installation
composer install
-
Include Autoloader into your project. Remember to replace
My_Appwith the value from step 3.require 'class-wp-autoloader.php'; new App\WP_Autoloader();
Class constructor accepts one optional parameter which is a relative path from a project root (where autoloader class located) to the folder with application files.
So, if you have your files in
application/framework/v2and its child folders, you can autoload it like:new App\WP_Autoloader(); $example_class = new App\Application\Framework\V2\Example();
or with specified folder path:
new App\WP_Autoloader( 'application/framework/v2' ); $example_class = new App\Example();
The WordPress Autoloader requires strict usage of following naming convention which is based on this article and is the following:
- Namespace names in a namespace declaration should not start with a leading backslash.
- Each part of a namespace and class, trait and interface names should be in
Camel_Caps, i.e. each word should start with a capital letter and words should be separated by an underscore. - The postfix
_Traitis required in a trait name; so does postfix_Interfacein an interface name. - Consecutive caps for acronyms allowed.
- Usage of numbers allowed, but each part of a namespace and class, trait and interface name must start with a letter.
- File names should be based on the correspondent class, trait or interface name with
class-,trait-orinterface-prepended, replacing each uppercase letter with lowercase letter, and the underscores in the name replaced with hyphens.
WordPress Autoloader comes with following Composer CLI commands:
- Check all
.phpfiles for syntax errors:composer lint:php
- Check
.phpfiles against PHP and WordPress Coding Standards:# Check all files. composer lint:wpcs # Check specific file. composer lint:wpcs -- path/to/my-file.php
- Automatically correct coding standard violations in
.phpfiles:# Fix violations that can be corrected in all files. composer lint:fix # Fix violations that can be corrected in specific file. composer lint:fix -- path/to/my-file.php
- Generate a
.potfile in thelanguages/directory:composer make-pot
Distributed under the MIT License. See LICENSE for more information.
Project Link: https://github.com/alexander-kozhukhovski/wordpress-autoloader