wppaste
WordPress

did_action( string $hook_name ): int

Since
2.1.0
Source
wp-includes/plugin.php:685

Returns how many times a given action hook has already fired during the current request, based on the global $wp_actions counter. Use it to guard code that should only run once a hook has completed, or to confirm a hook actually fired during a bulk operation. It does not tell you whether a callback is currently attached; for that use has_action() instead.

Retrieves the number of times an action has been fired during the current request.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$hook_namestring
The name of the action hook.

Return value

int
The number of times the action hook has been fired.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check whether the init hook has already fired

Guard logic that should behave differently depending on whether WordPress has finished the init phase.

$times = did_action( 'init' );

if ( $times > 0 ) {
	echo esc_html( sprintf( 'The init action has fired %d time(s) so far.', $times ) );
} else {
	echo esc_html( 'The init action has not fired yet in this request.' );
}

Confirm save_post fired once per post during a bulk update

Programmatically touch posts 1 through 5 and then check how many times save_post ran.

foreach ( range( 1, 5 ) as $post_id ) {
	wp_update_post(
		array(
			'ID'         => $post_id,
			'post_title' => get_the_title( $post_id ) . ' (touched)',
		)
	);
}

echo esc_html( sprintf( 'save_post fired %d time(s) during this request.', did_action( 'save_post' ) ) );

wp_update_post() fires save_post once per successful update, so the count reflects only updates made in this request, not any prior ones.

Common problems and fixes · 4

Why does did_action always return 0 for a hook I know runs on every page?

did_action() only counts fires that have already happened in the current request. If your code checks it before that hook point in the request lifecycle, $wp_actions has no entry yet and the function falls through to its 0 default.

What's the difference between did_action and has_action?

did_action() reads the $wp_actions counter and tells you how many times a hook has actually fired. has_action() instead checks whether a specific callback (or any callback) is registered to a hook, regardless of whether it has run yet.

Does did_action tell me if I'm currently inside that action right now?

Not directly. do_action() updates $wp_actions before it loops through the registered callbacks, so did_action() already reflects the current firing while it's in progress. To detect that you're actively inside a hook's callbacks, use doing_action() instead.

Will did_action remember counts from an earlier page load?

No. $wp_actions is a plain global reset on every request, so did_action() only ever reflects fires that happened since the current request started, never anything from a previous request or cron run.

Alternatives and related functions

has_action
When you need to know whether a specific callback is registered on a hook, regardless of whether that hook has fired yet.
doing_action
When you need to know if you are currently inside the execution of that hook's callbacks, not just whether it fired in the past.
current_action
When you need the name of whichever hook is presently executing, rather than a fire count for one specific hook.
did_filter
When you need the same fire count but for a filter hook instead of an action hook.

Performance profile

How much work a call to did_action() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–6

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 7.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
49

49 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost did_action() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–6none

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 7 instructions, 5–6 executed per call, 1 branch. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 49

Show all 49

Source code

function did_action( $hook_name ) {	global $wp_actions; 	if ( ! isset( $wp_actions[ $hook_name ] ) ) {		return 0;	} 	return $wp_actions[ $hook_name ];}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.