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.

Live output

Enable JavaScript to customise; default output below.

Hook type

A filter changes a value and must return it. An action does something and returns nothing.

The hook you are attaching to, such as the_content, init or wp_enqueue_scripts.

Prefix it. Two plugins with the same function name is a fatal error.

Lower runs earlier. 10 is the default; use 20 to run after most other callbacks.

Live preview hooks.php
<?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.

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

  1. Choose filter or action. A filter receives a value, changes it and must return it. An action does something and returns nothing.
  2. Name the hook you are attaching to, such as the_content, init or save_post.
  3. Add the arguments in the order the hook passes them. For a filter the first one is the value you return.
  4. Set Arguments passed to match the number of rows. WordPress only passes as many as that number says, whatever your function signature claims.
  5. 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() and remove_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. init callbacks added from a template are simply too late.
  • Function names are global. Two plugins defining my_callback is 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?
Internally they are the same system. The difference is the contract: a filter is given a value and must give one back, an action is told something happened and returns nothing.
Which priority should I use?
10 unless you have a reason. Use a higher number to run after other callbacks, lower to run before. Extremes such as 1 or 9999 are how two plugins end up fighting.
How do I know which arguments a hook passes?
Find the 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?
Usually the hook fires twice, as 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?
Yes: pass 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.

From the people who built this tool

WP Adminify

The WordPress admin, rebuilt: a dashboard worth looking at, menu and column control, a real file manager and the login page your client sees.

See WP Adminify Free version on WordPress.org

Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.