Paj Digital Labs
All articles
Web Design & Engineering

WordPress Hooks Are a Bus, Not a Plugin: Thinking in Actions and Filters

Most WordPress customization fails because people edit output instead of intercepting events. How the hook system actually works, and the three filters that replace most 'custom code' plugins.

Paj Digital Labs 2 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

The average WordPress site has a plugin for things that are three lines of code. A plugin to change the login logo. A plugin to remove the version string. A plugin to add a body class. Each one is an update channel, an attack surface, and a performance cost — in exchange for what a single filter does natively.

The reason this happens is that hooks are taught as a plugin-authoring API, when they are really the way WordPress itself is assembled. Core does not special-case its own behavior; it registers hooks like everyone else. Once you see that, customization stops being "find a plugin" and becomes "find the hook."

Actions and filters, precisely

An action is an event: "this happened, do something." A filter is a transformation: "here is a value, give me back a value." That is the entire system.

php
class="text-code-comment">// Action: after a post is saved, clear our cache
add_action(class="text-code-string">'save_post', function ($post_id) {
    delete_transient(class="text-code-string">'homepage_query');
});
class="text-code-comment">
// Filter: change the excerpt length
add_filter(class="text-code-string">'excerpt_length', fn() => 24);

Filters must return a value. The number of production bugs caused by a filter that echoes instead of returns is not small. If your filter returns nothing, every piece of content it touches becomes empty.

Priority and arguments are the real API

php
add_filter(class="text-code-string">'the_content', class="text-code-string">'my_function', 20, 2);

Priority decides order. The default is 10; core and plugins register at known priorities, and running at 20 means "after the normal stuff." Running at 1 means "before everything, including the things that fix the things." When two plugins fight, it is almost always a priority collision.

The argument count matters too. WordPress passes each hook a fixed number of arguments; if your function declares more than you asked for, you get a fatal error on PHP 8. Declare what you accept, accept what you declared.

Three filters that replace plugins

Remove the version string (a minor information leak, and one fewer thing in the head):

php
add_filter(class="text-code-string">'the_generator', class="text-code-string">'__return_empty_string');

Sane image sizes. Stop WordPress generating five crops nobody uses:

php
add_filter(class="text-code-string">'intermediate_image_sizes_advanced', function ($sizes) {
    unset($sizes[class="text-code-string">'medium_large'], $sizes[class="text-code-string">'1536x1536'], $sizes[class="text-code-string">'2048x2048']);
    return $sizes;
});

On a site with 10,000 images, this is the difference between a 4 GB uploads directory and a 14 GB one.

Clean the head. The emoji detection script alone is a render-blocking request on every page:

php
remove_action(class="text-code-string">'wp_head', class="text-code-string">'print_emoji_detection_script', 7);
remove_action(class="text-code-string">'wp_print_styles', class="text-code-string">'print_emoji_styles');

Note the priority on the first line: remove_action must match the priority the action was registered with, or it silently does nothing. "Silently does nothing" is the hook system's favorite failure mode.

Where the code goes

Not in functions.php of a theme you will replace — in a must-use plugin. A single file in wp-content/mu-plugins/site.php loads automatically, cannot be deactivated by a curious client, and survives theme changes. It is the correct home for everything that is about the site rather than the design.

The discipline

Before installing a plugin, ask: is this a feature, or is it a hook? If it is a hook, it is a file you own, code you can read, and one fewer supply chain to trust. The hook system is the reason WordPress survived twenty years. Use it directly.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading