wppaste
WordPress

add_action( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true

Since
1.2.0
Source
wp-includes/plugin.php:446

Attach a callback to a WordPress action hook with add_action() so your function runs when core, a theme, or a plugin fires that event. Priority sets run order (lower runs first) and $accepted_args must match the number of parameters your callback takes; the function always returns true.

Adds a callback function to an action hook.

Description

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Plugins can specify that one or more of its PHP functions are executed at these points, using the Action API.

Compatibility

WordPress
since 1.2.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 to add the callback to.
$callbackcallable
The callback to be run when the action is called.
$priorityintoptional
Used to specify the order in which the functions associated with a particular action are executed.
Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default 10.Default: 10
$accepted_argsintoptional
The number of arguments the function accepts. Default 1.Default: 1

Return value

true
Always returns true.

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.

Run setup code on init

Hook a function onto init to register a custom post type when WordPress boots.

add_action( 'init', 'myplugin_register_book_type' );

function myplugin_register_book_type() {
	register_post_type(
		'book',
		array(
			'label'    => 'Books',
			'public'   => true,
			'supports' => array( 'title', 'editor', 'thumbnail' ),
		)
	);
}

add_action() must run before the hook fires; a callback added after do_action( 'init' ) has already executed will never run.

Receive multiple arguments with $accepted_args

Listen for save_post and take all three arguments the hook passes so you can tell inserts from updates.

add_action( 'save_post', 'myplugin_log_save', 10, 3 );

function myplugin_log_save( $post_id, $post, $update ) {
	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	$verb = $update ? 'Updated' : 'Created';
	error_log( sprintf( '%s post %d: %s', $verb, $post_id, $post->post_title ) );
}

$accepted_args (here 3) must cover every required parameter your callback declares; with the default of 1, only $post_id is passed and PHP raises an ArgumentCountError.

Performance profile

How much work a call to add_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
11

Executed per call on PHP 8.5. The body compiles to 11.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 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 add_action() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11add_filter()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 11 instructions, 11 executed per call, 0 branches. 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.

Uses · 1

Used by · 50

Show all 50

Source code

function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {	return add_filter( $hook_name, $callback, $priority, $accepted_args );}

Changelog

Introduced in 1.2.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 7.0.4 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.