Action & Filter Hook Generator
Generate add_action() or add_filter() callbacks with the right signature, priority, argument count and a filter that returns its value.
<?php
/**
* Filters the_content.
*
* @param string $content
* @return string The filtered value.
*/
function my_plugin_filter_the_content( $content ) {
// Your code here.
return $content;
}
add_filter( 'the_content', 'my_plugin_filter_the_content', 10, 1 );
Output is valid and updates as you type.
Fix the highlighted fields to update the output.
Pick the hook, say whether it is an action or a filter, list the arguments it passes, and the generator writes the callback with the right signature and the add_action() or add_filter() line to match.
How to use
- Choose filter or action. A filter receives a value, changes it and must return it. An action does something and returns nothing.
- Name the hook you are attaching to, such as
the_content,initorsave_post. - Add the arguments in the order the hook passes them. For a filter the first one is the value you return.
- Set Arguments passed to match the number of rows. WordPress only passes as many as that number says, whatever your function signature claims.
- Pick a guard if the callback belongs to one context. It writes the early return, including the correct return value for a filter.
Example
An admin-only callback on save_post, running late so other plugins have finished, taking all three arguments the hook provides:
function acme_after_save_post( $post_id, $post, $update ) {
if ( ! is_admin() ) {
return;
}
// Your code here.
}
add_action( 'save_post', 'acme_after_save_post', 20, 3 );
The removal line is the part people get wrong later: remove_action() needs the same callback name and the same priority, or it silently removes nothing.
Pitfalls
- A filter that does not return a value returns null, and whatever it filtered becomes empty. This is the single most common way to blank out post content.
- Arguments passed is not decoration. Set it to 1 while your function expects three and PHP throws an ArgumentCountError on every run.
- Priority decides order, not importance. Two callbacks at the same priority run in the order they were added, which depends on plugin load order.
remove_action()andremove_filter()must match the priority used when adding. A removal at the default 10 cannot remove a callback added at 20.- Attaching to a hook that has already fired does nothing, silently.
initcallbacks added from a template are simply too late. - Function names are global. Two plugins defining
my_callbackis a fatal error, so prefix yours. is_admin()is true for admin-ajax requests, which is often not what “admin only” was meant to cover.- Guarding a filter with an early return means returning the value unchanged, not returning nothing. The generated guard does that for you.
Compatibility
add_action() and add_filter() are the oldest stable parts of WordPress and have not changed shape since 2.0. is_main_query() needs WordPress 3.3 or later. The generated code targets PHP 7.0 and up, and the tool runs entirely in your browser.
Frequently asked questions
What is the actual difference between an action and a filter?
Which priority should I use?
How do I know which arguments a hook passes?
do_action() or apply_filters() call in core or the plugin that fires it. The arguments after the hook name are what you receive, in that order.Why does my callback run twice?
save_post does for revisions and autosaves, rather than the callback being added twice. Guard on the post type or check for autosave.Can I attach a class method instead of a function?
array( $this, 'method' ) in place of the function name. Removing it later needs the same instance, which is why plain functions are easier to unhook.