WordPress Plugin Loader Tricks

An unusually short article, for me, on basic WordPress plugin loader tricks.

Basic setup

Name the “loader” php file the same as the plugin directory.

Text Domain must match the directory name.

Avoid leading * on header lines = less bytes to process by the header processor in WordPress.

Ensure it runs from within WordPress

Use function_exists( ‘add_action’ ) instead of defined( ‘ABSPATH’).  It is more likely to be specific to WordPress.   It is also a better test as the loader calls the add_action function and will break if it is AWOL.

Not doing heartbeat processing

Short circuit to avoid loading the main plugin code if your plugin has zero influence on heartbeat operations.    WordPress heartbeats fire up AJAX requests every minute or two when a user is on the site.

Load on plugins_loaded

The plugins_loaded action loads early enough to be able to latch onto actions that have to happen in init or admin_menu. If your plugin requires others to be loaded this ensures that has happened, just make sure you set the action priority higher than the plugin you rely.

Put code another level down

Bury most of the PHP code at least another level deeper than the loader.  I prefer an include subdirectory but any name will do.   This helps the WordPress plugin processor run faster during major updates or when the plugin cache expires.     WordPress reads the first 8k header of all PHP files within the main directory when this happens looking for the Plugin Name etc. headstone.

Define your file

Using define( <plugin_dir>_FILE , FILE ) gives you a consistent and fast way to reference the plugin’s install directory.

I like to define <plugin_dir>_DIR as well based on this location and point it to the include subdirectory to make it easier to find and reference code classes and resource files like JavaScript or CSS files we need later.

I prefer defined( <def_var> ) || define( <def_var> , <value> ) which is cleaner and easier to read than if(){} constructs.

An example ‘loader’ file

Plugin Name: AISwarm
Plugin URI: https://lancecleveland.com/
Description: AI Swarm plugin.
Author: Lance Cleveland
Author URI:
License: GPL3
Text Domain: aiswarm

Tested up to: 4.9.1
Version: 0.1

Copyright 2018  Lance Cleveland (lance@lance.bio)

// Die if not in WordPress version that supports add_action
if ( ! function_exists( 'add_action' ) ) {
    header( 'Status: 403 Forbidden' );
    header( 'HTTP/1.1 403 Forbidden' );

//   Do not load this plugin on WP heartbeat since we don't do anything with it.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST[ 'action' ] ) && ( $_POST[ 'action' ] === 'heartbeat' ) ) {

// Load the plugin.
function AISwarm_loader() {
    defined( 'AISWARM_FILE' ) || define( 'AISWARM_FILE', __FILE__ );
    defined( 'AISWARM_DIR'  ) || define( 'AISWARM_DIR' , dirname( __FILE__ ) . '/include/' );

    require_once( AISWARM_DIR . 'AISwarm.php' );

    global $aiswarm;
    $aiswarm = new AISwarm();
add_action( 'plugins_loaded' , 'AISwarm_loader' );

See something wrong or have some other WordPress plugin loader trick to share?  Hit me up in the comments.

One thought on “WordPress Plugin Loader Tricks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.