Skip to content

whoami-pwd/wordpress-autoloader

Repository files navigation


WordPress-Autoloader

Class, Trait and Interface Autoloader for WordPress

Report Bug or Suggest Improvement

Table of Contents
  1. About The Project
  2. Getting Started
  3. Naming Convention
  4. Available CLI commands
  5. License
  6. Contact
  7. Acknowledgments

About The Project

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.

Requirements

(back to top)

Getting Started

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.

Prerequisites

Make sure you have installed:

  • PHP 7.4+
  • Composer

Installation

  1. Clone or download the repo into root of your WordPress theme or plugin folder:

    git clone https://github.com/alexander-kozhukhovski/wordpress-autoloader.git
  2. Remove .git folder and .gitignore file (assume that you already have configured Git in your project):

    rm -r .git
    rm .gitignore
  3. Complete following find-and-replace actions in phpcs.xml.dist, composer.json and class-wp-autoloader.php files:

    • Search for my-app to capture the text domain and .pot file and replace it with your values
    • Search for My_App to capture initial namespace and DocBlocks and replace it with your values
  4. Run composer installation

    composer install
  5. Include Autoloader into your project. Remember to replace My_App with 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/v2 and 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();

(back to top)

Naming Convention

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 _Trait is required in a trait name; so does postfix _Interface in 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- or interface- prepended, replacing each uppercase letter with lowercase letter, and the underscores in the name replaced with hyphens.

(back to top)

Available CLI commands

WordPress Autoloader comes with following Composer CLI commands:

  • Check all .php files for syntax errors:
    composer lint:php
  • Check .php files 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 .php files:
    # 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 .pot file in the languages/ directory:
    composer make-pot

(back to top)

License

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

LinkedIn

Project Link: https://github.com/alexander-kozhukhovski/wordpress-autoloader

(back to top)

Acknowledgments

(back to top)

About

Class, Trait and Interface Autoloader for WordPress

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages