Choosing a Documentation Method for a WordPress Plugin
People look at a WordPress plugin at different stages. Some are deciding whether to install it, others are setting it up, and…
A WordPress plugin adds or changes how a site behaves. Instead of editing WordPress Core files, which updates can overwrite, you connect plugin code to Core through hooks. A hook is a named point in execution where WordPress runs code that Core, a theme, or a plugin has registered.
When WordPress reaches a hook, it looks up every callback registered there and runs each one. A callback is a function or method attached to that hook. Some hooks expect your callback to perform a task, while others expect it to return a changed value. Once you know which kind of hook you are working with, you can pick the right API and write code that behaves as WordPress expects.
A plugin registers callbacks with add_action() or add_filter(). When a plugin registers a callback, it tells WordPress what code to call and when. WordPress stores that callback and waits; it runs the callback only when it reaches the matching hook.
For example, a plugin can attach a callback to init, a hook WordPress reaches during request startup:
function acme_register_features() {
// Register plugin features here.
}
add_action( 'init', 'acme_register_features' );
During a standard request, WordPress loads active plugins, fires plugins_loaded, and later reaches init. Your plugin must register its callback before that hook fires, or WordPress will not call it during that request. The Core bootstrap code shows this order.
A plugin can be as small as a single PHP file with a plugin header, or it can split its code across many files. Either way, hooks let you extend WordPress through its supported interfaces rather than editing Core directly. The Plugin Handbook’s overview covers plugin structure and setup.
Use an action when you want a callback to perform a task at a particular point. WordPress triggers an action with do_action(), and a plugin registers a callback with add_action(). WordPress ignores whatever value an action callback returns, so it does not replace anything in the calling code.
For instance, WordPress runs save_post whenever it saves a post. That hook passes three values: the post ID, the post object, and a flag showing whether WordPress is updating an existing post. To receive all three, your callback must declare all three parameters, and you must tell add_action() to pass three arguments:
function acme_after_post_save( $post_id, $post, $update ) {
// Perform a task related to this post save.
}
add_action( 'save_post', 'acme_after_post_save', 10, 3 );
Use a filter when you want a callback to inspect or change a value. WordPress triggers a filter with apply_filters(), and your callback returns the value WordPress should use next. If several callbacks modify the same value, WordPress passes each callback the result returned by the previous one. The Hooks Handbook explains this distinction and what each type expects.
This filter adds a note to a post’s content when WordPress displays a single post through the main loop, its standard way of displaying posts:
function acme_add_reading_note( $content ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
return $content . '<p class="reading-note">End of article.</p>';
}
add_filter( 'the_content', 'acme_add_reading_note', 20 );
The callback returns the original content when the conditions do not match, and it returns the updated content when they do. Every filter callback must return a value, even when it makes no changes. Keep filter callbacks focused on transforming that value. The Hooks Handbook guidance advises against using filters for side effects such as printing output or changing global variables.
The third argument to add_action() or add_filter() sets the callback’s priority, a number that tells WordPress when to run it compared with other callbacks on the same hook. Lower numbers run earlier. WordPress uses priority 10 by default, and it runs callbacks with the same priority in the order plugins registered them.
The fourth argument sets accepted_args, the number of hook arguments WordPress passes to your callback. Its default is 1. If a hook supplies three arguments and you leave accepted_args at 1, WordPress passes only the first argument to your callback. Check the hook’s documentation or find its do_action() or apply_filters() call in the code before relying on its arguments. The add_action() reference documents all of the registration parameters.
You can raise a callback’s priority number to run it later, but that does not guarantee it will run last. Another plugin can register a callback with an even higher number, or register one at the same priority after yours. When a filter’s result depends on another callback, inspect that other registration and test both together.
Plugins can expose their own extension points, or places where other code can connect, with do_action() and apply_filters(). For example, a plugin that builds a configuration array can pass it through a filter before using it, giving other plugins a chance to adjust the settings. An action lets other code run when your plugin reaches a specific event.
Give custom hooks a specific, prefixed name so they do not collide with hooks from Core or other plugins. The Custom Hooks guidance explains how plugins create and document these extension points.
A plugin can also remove a callback with remove_action() or remove_filter(). WordPress removes the callback only if you pass the exact same hook name, callback, and priority, and only if your code runs after the original plugin registered that callback. Because removing another component’s callback changes how it behaves, test the result in the context where the hook runs. See the advanced hook guidance for more detail.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com