Skip to content

Working with the Abstract Plugin Base

David Ryan edited this page Jan 8, 2018 · 3 revisions

The Abstract Plugin Base provides a standard hierarchy for organizing WordPress code adept for plugins small and large.

  1. A main plugin file sits in the root directory. This has the WordPress Front Matter to register the plugin, dependency enqueues for the plugin base (and perhaps other tools like our options panel).
<?php
/**
 * Plugin Name: Custom My Plugin
 * Plugin URI: https://github.com/
 */

//avoid direct calls to this file, because now WP core and framework has been used
if ( ! function_exists( 'add_filter' ) ) {
	header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
	exit();
}
// Create plugin instance on plugins_loaded action to maximize flexibility of wp hooks and filters system.
include_once 'vendor/autoload.php';
include_once 'app/class-my-plugin.php';
Custom\My_Plugin\App::run( __FILE__ );
  1. The main file points to a class that extends the Abstract Plugin Base -- this is our main plugin file where we initialize other functionality.

    • Small plugins may just have a few functions in this class.
    • Medium-size plugins we recommend initialize sub-classes split between an init() and authenticated_init() method in the main class.
    • Large plugins we recommend include a class-public.php and class-authenticated.php to organize these sub-class initializations to help keep the main file lean.
  2. The Abstract Plugin Base helps load code in the WordPress Hook Cascade at a predictable time.

Adding functionality

Built-in autoloading makes adding new files easy and hassle-free! We'll update more later, but if you use PSR-4 namespace naming conventions and store your code in /app then files will automatically load.

Example (with namespace MyOrg\MyPlugin)
  • /app/class-my-example-class.php can be called by MyOrg\MyPlugin\MyExampleClass()
  • /app/includes/class-my-public-class.php can be called by MyOrg\MyPlugin\Includes\MyPublicClass()
  • /app/admin/class-my-admin-class.php can be called by MyOrg\MyPlugin\Admin\MyAdminClass()
Clone this wiki locally