WordPress

Action

Action is a WordPress hook that fires at a named point and runs your function for its side effects. Nothing the callback returns is used, so it is the right tool for sending mail, writing a row, or enqueuing a script.

How it is measured

You do not count an action, you confirm it fires. Registration is add_action( 'hook_name', 'callback', priority, accepted_args ) with priority defaulting to 10 and accepted_args to 1. At runtime did_action( 'init' ) tells you how many times a hook has fired in this request, and has_action() returns the priority of an attached callback or false.

Query Monitor's Hooks & Actions panel lists every callback bound to each action on the current page, in priority order, with the plugin that owns it. That is the quickest way to see who else is listening on save_post or wp_footer.

Worked example

A WooCommerce shop mails its warehouse when an order reaches processing. A developer binds send_warehouse_email to woocommerce_order_status_processing at priority 10. Query Monitor shows 14 callbacks on that action and theirs takes 1.9 seconds because it opens an SMTP connection inline, so the customer sits on the thank-you page while it runs.

Moving the send into a one-off event with wp_schedule_single_event() drops the callback to 11 ms. The mail still goes out, just a minute later via WP-Cron.

How it differs

An action is one kind of hook. The word hook covers both actions and filters, and the registration call looks almost identical. The difference is the return path: an action's return value is thrown away, while a filter must hand a value back or the content it wraps breaks.

Common errors

Typing the hook name wrong, so the callback never runs and nothing warns you. Forgetting the fourth argument of add_action() and receiving only the first parameter. Returning a value from an action and expecting it to change output. Attaching to init something that needs a plugin that loads later. Echoing text from a callback that fires during an admin-ajax.php request and corrupting the JSON.

In practice

This week, open Query Monitor on your slowest front-end page and sort the Hooks panel by time. Any action callback over 100 ms that does network work, such as calling a CRM or sending mail, is a candidate to push into a scheduled event. Keep callbacks in a named function or class method so you can find and remove them with remove_action() later.

See also

Hook, Filter

Sources

Count this on a real site.

Watch my website