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.
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
- Scaling
- Constant
- Instructions
- 11
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 11.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | add_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
- add_filter()Adds a callback function to a filter hook.
Used by · 50
- Custom_Background::__construct()Constructor - Registers administration header callback.
- Custom_Background::init()Sets up the hooks for the Custom Background admin page.
- Custom_Image_Header::__construct()Constructor - Registers administration header callback.
- Custom_Image_Header::init()Sets up the hooks for the Custom Header admin page.
- Featured_Content::init()Conditionally hooks into WordPress.
- Featured_Content::setup()Instantiates.
- Language_Pack_Upgrader::bulk_upgrade()Upgrades several language packs at once.
- Plugin_Upgrader::install()Install a plugin package.
- Plugin_Upgrader::upgrade()Upgrades a plugin.
- Theme_Upgrader::install()Install a theme package.
- Theme_Upgrader::upgrade()Upgrades a theme.
- Twenty_Eleven_Ephemera_Widget::__construct()PHP5 constructor.
Show all 50
- Twenty_Fourteen_Ephemera_Widget::__construct()Constructor.
- Twenty_Twenty_One_Custom_Colors::__construct()Instantiates the object.
- Twenty_Twenty_One_Customize::__construct()Constructor. Instantiates the object.
- Twenty_Twenty_One_Dark_Mode::__construct()Instantiates the object.
- WP_Admin_Bar::add_menus()Adds menus to the admin bar.
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Customize_Header_Image_Control::prepare_control()
- WP_Customize_Manager::__construct()Constructor.
- WP_Customize_Manager::customize_preview_init()Prints JavaScript settings.
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- WP_Customize_Manager::setup_theme()Starts preview and customize theme.
- WP_Customize_Nav_Menu_Item_Setting::__construct()Constructor.
- WP_Customize_Nav_Menus::__construct()Constructor.
- WP_Customize_Nav_Menus::customize_preview_init()Adds hooks for the Customizer preview.
- WP_Customize_Selective_Refresh::__construct()Plugin bootstrap for Partial Refresh functionality.
- WP_Customize_Selective_Refresh::enqueue_preview_scripts()Enqueues preview scripts.
- WP_Customize_Selective_Refresh::init_preview()Initializes the Customizer preview.
- WP_Customize_Setting::aggregate_multidimensional()Set up the setting for aggregated multidimensional values.
- WP_Customize_Setting::preview()Add filters to supply the setting's value when accessed.
- WP_Customize_Site_Icon_Control::__construct()Constructor.
- WP_Customize_Widgets::__construct()Initial loader.
- WP_Customize_Widgets::customize_preview_init()Adds hooks for the Customizer preview.
- WP_Customize_Widgets::schedule_customize_register()Ensures widgets are available for all types of previews.
- WP_Customize_Widgets::selective_refresh_init()Adds hooks for selective refresh.
- WP_Embed::__construct()Constructor
- WP_Interactivity_API::data_wp_router_region_processor()Processes the `data-wp-router-region` directive.
- WP_Internal_Pointers::enqueue_scripts()Initializes the new feature pointers.
- WP_List_Table::__construct()Constructor.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Post_Type::add_hooks()Adds the future post hook action for the post type.
- WP_Post_Type::register_meta_boxes()Registers the post type meta box if a custom callback was specified.
- WP_Privacy_Policy_Content::text_change_check()Performs a quick check to determine whether any privacy info has changed.
- WP_Recovery_Mode::initialize()Initialize recovery mode for the current request.
- WP_Rewrite::flush_rules()Removes rewrite rules and then recreate rewrite rules.
- WP_Rewrite::refresh_rewrite_rules()Refreshes the rewrite rules, saving the fresh value to the database.
- WP_Roles::get_roles_data()Gets the available roles data.
- WP_Script_Modules::add_hooks()Adds the hooks to print the import map, enqueued script modules and script module preloads.
- WP_Scripts::__construct()Constructor.
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.
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.