Lesson 1 of 9 · 24m
Hooks and Plugin Architecture: Joining the Request, Not Owning It
The action/filter mental model, correct bootstrapping, and why professional plugins are thin integrations into WordPress rather than little frameworks.
A WordPress plugin is not an application with WordPress inside it. It is a guest inside a long-running PHP request lifecycle that WordPress owns. The plugins that age well understand this: they register callbacks, keep bootstrap cheap, and do work only when the current request actually needs it.
The disasters usually look different. I have reviewed plugins that connected to a remote API in their main file, created database tables before plugins_loaded, and enqueued slider assets on every page of the internet because nobody asked when code should run. The symptoms look random — slow admin pages, broken embeds, fatal errors in wp-cron — but the cause is always the same: the plugin acted like it owned the request.
Actions and filters are the extension API
An action says something happened here; react if you care. A filter says here is a value; transform it and return a value. That distinction matters more than syntax.
add_action('init', 'acme_register_types');
add_filter('the_content', 'acme_embed_card');
function acme_embed_card(string $content): string {
if (is_singular('product') && in_the_loop()) {
$content .= '<aside class="acme-card">' . esc_html__('Supported by Acme', 'acme') . '</aside>';
}
return $content;
}Filter callbacks must always return the filtered type. Returning nothing from the_content deletes the post content. Returning early from a filter because a request is a REST request can silently break syndication, previews, or headless consumers.
Bootstrap should register, not execute
<?php
/**
* Plugin Name: Acme Inventory
*/
if (!defined('ABSPATH')) {
exit;
}
define('ACME_INVENTORY_VERSION', '1.4.0');
add_action('plugins_loaded', 'acme_inventory_bootstrap');
function acme_inventory_bootstrap(): void {
require_once __DIR__ . '/includes/class-plugin.php';
Acme\Inventory\Plugin::instance()->register();
}The main file should establish constants, guards, and hook registrations. It should not query the database, load every class in the plugin, inspect options, or call remote services. In large sites, dozens of plugins load on every request; the plugin that takes 80 ms just to declare itself becomes the reason a checkout page misses its cache window.
Use the right hook for the layer
plugins_loaded— plugin services are available; too early for almost everything user-facing.init— register post types, taxonomies, rewrite-dependent structures.wp_enqueue_scripts— register and enqueue front-end assets; never print scripts directly.admin_init/admin_menu— settings and screens in wp-admin.rest_api_init— REST routes.wp— the main query is known; use it for conditional front-end logic.
Priority and argument count are part of the API. add_filter('pre_option_acme_key', $callback, 10, 3) means the callback receives three arguments. If you omit 3, PHP gets fewer parameters than your function expects and modern PHP turns that into a fatal.
Architecture: services behind hooks
A clean plugin usually has a small Plugin coordinator, feature classes with register() methods, and pure domain logic that does not know about the current hook. Static singletons are common in WordPress; use them to prevent duplicate hook registration, not as a dumping ground for global mutable state.
Field notes
- Never echo during plugin load; unexpected output breaks headers, feeds, and REST responses.
- Check
did_action()only for diagnostics. Correct hook choice beats clever ordering detection. - Use named functions or static methods unless a closure truly must capture local state. Named callbacks are removable and testable.
- If your callback depends on post type registration, remember that registration belongs on
initeven when the data exists in the database.
Was this lesson helpful?