Title: add_action
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#more-information)
    - [Usage](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#usage)
 * [Source](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#wp--skip-link--target)

Adds a callback function to an action hook.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#parameters)󠁿

 `$hook_name`stringrequired

The name of the action to add the callback to.

`$callback`callablerequired

The callback to be run when the action is called.

`$priority`intoptional

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`

`$accepted_args`intoptional

The number of arguments the function accepts.

Default:`1`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#return)󠁿

 true Always returns true.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#more-information)󠁿

### 󠀁[Usage](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#usage)󠁿

    ```php
    add_action( $hook, $function_to_add, $priority, $accepted_args );
    ```

To find out the number and name of arguments for an action, simply search the code
base for the matching [do_action()](https://developer.wordpress.org/reference/functions/do_action/)
call. For example, if you are hooking into ‘save_post’, you would find it in post.
php:

    ```php
    do_action( 'save_post', $post_ID, $post, $update );
    ```

Your add_action call would look like:

    ```php
    add_action( 'save_post', 'wpdocs_my_save_post', 10, 3 );
    ```

And your function would be:

    ```php
    function wpdocs_my_save_post( $post_ID, $post, $update ) {
    // do stuff here
    }
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/plugin.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/plugin.php#L447)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/plugin.php#L447-L449)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#related)󠁿

| Uses | Description | 
| [add_filter()](https://developer.wordpress.org/reference/functions/add_filter/)`wp-includes/plugin.php` |

Adds a callback function to a filter hook.

  |

| Used by | Description | 
| [wp_load_classic_theme_block_styles_on_demand()](https://developer.wordpress.org/reference/functions/wp_load_classic_theme_block_styles_on_demand/)`wp-includes/script-loader.php` |

Adds hooks to load block styles on demand in classic themes.

  | 
| [wp_hoist_late_printed_styles()](https://developer.wordpress.org/reference/functions/wp_hoist_late_printed_styles/)`wp-includes/script-loader.php` |

Adds the hooks needed for CSS output to be delayed until after the content of the page has been established.

  | 
| [WP_Interactivity_API::data_wp_router_region_processor()](https://developer.wordpress.org/reference/classes/wp_interactivity_api/data_wp_router_region_processor/)`wp-includes/interactivity-api/class-wp-interactivity-api.php` |

Processes the `data-wp-router-region` directive.

  | 
| [WP_Script_Modules::add_hooks()](https://developer.wordpress.org/reference/classes/wp_script_modules/add_hooks/)`wp-includes/class-wp-script-modules.php` |

Adds the hooks to print the import map, enqueued script modules and script module preloads.

  | 
| [WP_Textdomain_Registry::init()](https://developer.wordpress.org/reference/classes/wp_textdomain_registry/init/)`wp-includes/class-wp-textdomain-registry.php` |

Initializes the registry.

  | 
| [wp_initialize_theme_preview_hooks()](https://developer.wordpress.org/reference/functions/wp_initialize_theme_preview_hooks/)`wp-includes/theme-previews.php` |

Add filters and actions to enable Block Theme Previews in the Site Editor.

  | 
| [WP_Rewrite::refresh_rewrite_rules()](https://developer.wordpress.org/reference/classes/wp_rewrite/refresh_rewrite_rules/)`wp-includes/class-wp-rewrite.php` |

Refreshes the rewrite rules, saving the fresh value to the database.

  | 
| [wp_delete_all_temp_backups()](https://developer.wordpress.org/reference/functions/wp_delete_all_temp_backups/)`wp-includes/update.php` |

Schedules the removal of all contents in the temporary backup directory.

  | 
| [_wp_get_iframed_editor_assets()](https://developer.wordpress.org/reference/functions/_wp_get_iframed_editor_assets/)`wp-includes/block-editor.php` |

Collect the block editor assets that need to be loaded into the editor’s iframe.

  | 
| [_wp_theme_json_webfonts_handler()](https://developer.wordpress.org/reference/functions/_wp_theme_json_webfonts_handler/)`wp-includes/deprecated.php` |

Runs the theme.json webfonts handler.

  | 
| [wp_enqueue_block_support_styles()](https://developer.wordpress.org/reference/functions/wp_enqueue_block_support_styles/)`wp-includes/script-loader.php` |

Hooks inline styles in the proper place, depending on the active theme.

  | 
| [_add_template_loader_filters()](https://developer.wordpress.org/reference/functions/_add_template_loader_filters/)`wp-includes/block-template.php` |

Adds necessary hooks to resolve ‘_wp-find-template’ requests.

  | 
| [wp_enqueue_block_style()](https://developer.wordpress.org/reference/functions/wp_enqueue_block_style/)`wp-includes/script-loader.php` |

Enqueues a stylesheet for a specific block.

  | 
| [locate_block_template()](https://developer.wordpress.org/reference/functions/locate_block_template/)`wp-includes/block-template.php` |

Finds a block template with equal or higher specificity than a given PHP template file.

  | 
| [WP_Sitemaps::init()](https://developer.wordpress.org/reference/classes/wp_sitemaps/init/)`wp-includes/sitemaps/class-wp-sitemaps.php` |

Initiates all sitemap functionality.

  | 
| [WP_Recovery_Mode::initialize()](https://developer.wordpress.org/reference/classes/wp_recovery_mode/initialize/)`wp-includes/class-wp-recovery-mode.php` |

Initialize recovery mode for the current request.

  | 
| [WP_Site_Health::__construct()](https://developer.wordpress.org/reference/classes/wp_site_health/__construct/)`wp-admin/includes/class-wp-site-health.php` |

[WP_Site_Health](https://developer.wordpress.org/reference/classes/wp_site_health/) constructor.

  | 
| [wp_is_site_initialized()](https://developer.wordpress.org/reference/functions/wp_is_site_initialized/)`wp-includes/ms-site.php` |

Checks whether a site is initialized.

  | 
| [register_and_do_post_meta_boxes()](https://developer.wordpress.org/reference/functions/register_and_do_post_meta_boxes/)`wp-admin/includes/meta-boxes.php` |

Registers the default post meta boxes, and runs the `do_meta_boxes` actions.

  | 
| [WP_Privacy_Policy_Content::text_change_check()](https://developer.wordpress.org/reference/classes/wp_privacy_policy_content/text_change_check/)`wp-admin/includes/class-wp-privacy-policy-content.php` |

Performs a quick check to determine whether any privacy info has changed.

  | 
| [WP_Roles::get_roles_data()](https://developer.wordpress.org/reference/classes/wp_roles/get_roles_data/)`wp-includes/class-wp-roles.php` |

Gets the available roles data.

  | 
| [WP_Widget_Text::_register_one()](https://developer.wordpress.org/reference/classes/wp_widget_text/_register_one/)`wp-includes/widgets/class-wp-widget-text.php` |

Adds hooks for enqueueing assets when registering all widget instances of this widget class.

  | 
| [WP_Widget_Custom_HTML::_register_one()](https://developer.wordpress.org/reference/classes/wp_widget_custom_html/_register_one/)`wp-includes/widgets/class-wp-widget-custom-html.php` |

Add hooks for enqueueing assets when registering all widget instances of this widget class.

  | 
| [WP_Widget_Media::_register_one()](https://developer.wordpress.org/reference/classes/wp_widget_media/_register_one/)`wp-includes/widgets/class-wp-widget-media.php` |

Add hooks while registering all widget instances of this widget class.

  | 
| [_WP_Editors::enqueue_default_editor()](https://developer.wordpress.org/reference/classes/_wp_editors/enqueue_default_editor/)`wp-includes/class-wp-editor.php` |

Enqueue all editor scripts.

  | 
| [_wp_delete_customize_changeset_dependent_auto_drafts()](https://developer.wordpress.org/reference/functions/_wp_delete_customize_changeset_dependent_auto_drafts/)`wp-includes/nav-menu.php` |

Deletes auto-draft posts associated with the supplied changeset.

  | 
| [WP_Customize_Manager::import_theme_starter_content()](https://developer.wordpress.org/reference/classes/wp_customize_manager/import_theme_starter_content/)`wp-includes/class-wp-customize-manager.php` |

Imports theme starter content into the customized state.

  | 
| [WP_Post_Type::register_meta_boxes()](https://developer.wordpress.org/reference/classes/wp_post_type/register_meta_boxes/)`wp-includes/class-wp-post-type.php` |

Registers the post type meta box if a custom callback was specified.

  | 
| [WP_Post_Type::add_hooks()](https://developer.wordpress.org/reference/classes/wp_post_type/add_hooks/)`wp-includes/class-wp-post-type.php` |

Adds the future post hook action for the post type.

  | 
| [WP_Customize_Widgets::selective_refresh_init()](https://developer.wordpress.org/reference/classes/wp_customize_widgets/selective_refresh_init/)`wp-includes/class-wp-customize-widgets.php` |

Adds hooks for selective refresh.

  | 
| [WP_Customize_Selective_Refresh::init_preview()](https://developer.wordpress.org/reference/classes/wp_customize_selective_refresh/init_preview/)`wp-includes/customize/class-wp-customize-selective-refresh.php` |

Initializes the Customizer preview.

  | 
| [WP_Customize_Selective_Refresh::enqueue_preview_scripts()](https://developer.wordpress.org/reference/classes/wp_customize_selective_refresh/enqueue_preview_scripts/)`wp-includes/customize/class-wp-customize-selective-refresh.php` |

Enqueues preview scripts.

  | 
| [WP_Customize_Selective_Refresh::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_selective_refresh/__construct/)`wp-includes/customize/class-wp-customize-selective-refresh.php` |

Plugin bootstrap for Partial Refresh functionality.

  | 
| [rest_api_default_filters()](https://developer.wordpress.org/reference/functions/rest_api_default_filters/)`wp-includes/rest-api.php` |

Registers the default REST API filters.

  | 
| [WP_Customize_Setting::aggregate_multidimensional()](https://developer.wordpress.org/reference/classes/wp_customize_setting/aggregate_multidimensional/)`wp-includes/class-wp-customize-setting.php` |

Set up the setting for aggregated multidimensional values.

  | 
| [wp_admin_bar_customize_menu()](https://developer.wordpress.org/reference/functions/wp_admin_bar_customize_menu/)`wp-includes/admin-bar.php` |

Adds the “Customize” link to the Toolbar.

  | 
| [WP_Widget_Factory::__construct()](https://developer.wordpress.org/reference/classes/wp_widget_factory/__construct/)`wp-includes/class-wp-widget-factory.php` |

PHP5 constructor.

  | 
| [WP_Customize_Site_Icon_Control::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_site_icon_control/__construct/)`wp-includes/customize/class-wp-customize-site-icon-control.php` |

Constructor.

  | 
| [WP_Customize_Nav_Menu_Item_Setting::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_nav_menu_item_setting/__construct/)`wp-includes/customize/class-wp-customize-nav-menu-item-setting.php` |

Constructor.

  | 
| [WP_Customize_Nav_Menus::customize_preview_init()](https://developer.wordpress.org/reference/classes/wp_customize_nav_menus/customize_preview_init/)`wp-includes/class-wp-customize-nav-menus.php` |

Adds hooks for the Customizer preview.

  | 
| [WP_Customize_Nav_Menus::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_nav_menus/__construct/)`wp-includes/class-wp-customize-nav-menus.php` |

Constructor.

  | 
| [WP_Site_Icon::__construct()](https://developer.wordpress.org/reference/classes/wp_site_icon/__construct/)`wp-admin/includes/class-wp-site-icon.php` |

Registers actions and filters.

  | 
| [print_emoji_detection_script()](https://developer.wordpress.org/reference/functions/print_emoji_detection_script/)`wp-includes/formatting.php` |

Prints the inline Emoji detection script if it is not already printed.

  | 
| [login_header()](https://developer.wordpress.org/reference/functions/login_header/)`wp-login.php` |

Outputs the login page header.

  | 
| [Language_Pack_Upgrader::bulk_upgrade()](https://developer.wordpress.org/reference/classes/language_pack_upgrader/bulk_upgrade/)`wp-admin/includes/class-language-pack-upgrader.php` |

Upgrades several language packs at once.

  | 
| [Theme_Upgrader::install()](https://developer.wordpress.org/reference/classes/theme_upgrader/install/)`wp-admin/includes/class-theme-upgrader.php` |

Install a theme package.

  | 
| [Theme_Upgrader::upgrade()](https://developer.wordpress.org/reference/classes/theme_upgrader/upgrade/)`wp-admin/includes/class-theme-upgrader.php` |

Upgrades a theme.

  | 
| [Plugin_Upgrader::install()](https://developer.wordpress.org/reference/classes/plugin_upgrader/install/)`wp-admin/includes/class-plugin-upgrader.php` |

Install a plugin package.

  | 
| [Plugin_Upgrader::upgrade()](https://developer.wordpress.org/reference/classes/plugin_upgrader/upgrade/)`wp-admin/includes/class-plugin-upgrader.php` |

Upgrades a plugin.

  | 
| [WP_Upgrader::run()](https://developer.wordpress.org/reference/classes/wp_upgrader/run/)`wp-admin/includes/class-wp-upgrader.php` |

Runs an upgrade/installation.

  | 
| [WP_List_Table::__construct()](https://developer.wordpress.org/reference/classes/wp_list_table/__construct/)`wp-admin/includes/class-wp-list-table.php` |

Constructor.

  | 
| [WP_Theme_Install_List_Table::prepare_items()](https://developer.wordpress.org/reference/classes/wp_theme_install_list_table/prepare_items/)`wp-admin/includes/class-wp-theme-install-list-table.php` |  | 
| [wp_plugin_update_rows()](https://developer.wordpress.org/reference/functions/wp_plugin_update_rows/)`wp-admin/includes/update.php` |

Adds a callback to display update information for plugins with updates available.

  | 
| [wp_theme_update_rows()](https://developer.wordpress.org/reference/functions/wp_theme_update_rows/)`wp-admin/includes/update.php` |

Adds a callback to display update information for themes with updates available.

  | 
| [uninstall_plugin()](https://developer.wordpress.org/reference/functions/uninstall_plugin/)`wp-admin/includes/plugin.php` |

Uninstalls a single plugin.

  | 
| [add_menu_page()](https://developer.wordpress.org/reference/functions/add_menu_page/)`wp-admin/includes/plugin.php` |

Adds a top-level menu page.

  | 
| [add_submenu_page()](https://developer.wordpress.org/reference/functions/add_submenu_page/)`wp-admin/includes/plugin.php` |

Adds a submenu page.

  | 
| [WP_Plugin_Install_List_Table::prepare_items()](https://developer.wordpress.org/reference/classes/wp_plugin_install_list_table/prepare_items/)`wp-admin/includes/class-wp-plugin-install-list-table.php` |  | 
| [WP_Internal_Pointers::enqueue_scripts()](https://developer.wordpress.org/reference/classes/wp_internal_pointers/enqueue_scripts/)`wp-admin/includes/class-wp-internal-pointers.php` |

Initializes the new feature pointers.

  | 
| [Custom_Image_Header::__construct()](https://developer.wordpress.org/reference/classes/custom_image_header/__construct/)`wp-admin/includes/class-custom-image-header.php` |

Constructor – Registers administration header callback.

  | 
| [Custom_Image_Header::init()](https://developer.wordpress.org/reference/classes/custom_image_header/init/)`wp-admin/includes/class-custom-image-header.php` |

Sets up the hooks for the Custom Header admin page.

  | 
| [Custom_Background::__construct()](https://developer.wordpress.org/reference/classes/custom_background/__construct/)`wp-admin/includes/class-custom-background.php` |

Constructor – Registers administration header callback.

  | 
| [Custom_Background::init()](https://developer.wordpress.org/reference/classes/custom_background/init/)`wp-admin/includes/class-custom-background.php` |

Sets up the hooks for the Custom Background admin page.

  | 
| [WP_Customize_Manager::customize_preview_init()](https://developer.wordpress.org/reference/classes/wp_customize_manager/customize_preview_init/)`wp-includes/class-wp-customize-manager.php` |

Prints JavaScript settings.

  | 
| [WP_Customize_Manager::setup_theme()](https://developer.wordpress.org/reference/classes/wp_customize_manager/setup_theme/)`wp-includes/class-wp-customize-manager.php` |

Starts preview and customize theme.

  | 
| [WP_Customize_Manager::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_manager/__construct/)`wp-includes/class-wp-customize-manager.php` |

Constructor.

  | 
| [wp_cron()](https://developer.wordpress.org/reference/functions/wp_cron/)`wp-includes/cron.php` |

Registers [_wp_cron()](https://developer.wordpress.org/reference/functions/_wp_cron/) to run on the [‘shutdown’](https://developer.wordpress.org/reference/hooks/shutdown/) action.

  | 
| [_custom_header_background_just_in_time()](https://developer.wordpress.org/reference/functions/_custom_header_background_just_in_time/)`wp-includes/theme.php` |

Registers the internal custom header and background routines.

  | 
| [add_thickbox()](https://developer.wordpress.org/reference/functions/add_thickbox/)`wp-includes/general-template.php` |

Enqueues the default ThickBox js and css.

  | 
| [wp_auth_check_load()](https://developer.wordpress.org/reference/functions/wp_auth_check_load/)`wp-includes/functions.php` |

Loads the auth check for monitoring whether the user is still logged in.

  | 
| [wp_maybe_load_widgets()](https://developer.wordpress.org/reference/functions/wp_maybe_load_widgets/)`wp-includes/functions.php` |

Determines if Widgets library should be loaded.

  | 
| [WP_Widget_Recent_Comments::__construct()](https://developer.wordpress.org/reference/classes/wp_widget_recent_comments/__construct/)`wp-includes/widgets/class-wp-widget-recent-comments.php` |

Sets up a new Recent Comments widget instance.

  | 
| [WP_Embed::__construct()](https://developer.wordpress.org/reference/classes/wp_embed/__construct/)`wp-includes/class-wp-embed.php` |

Constructor

  | 
| [WP_Admin_Bar::add_menus()](https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menus/)`wp-includes/class-wp-admin-bar.php` |

Adds menus to the admin bar.

  | 
| [WP_Admin_Bar::initialize()](https://developer.wordpress.org/reference/classes/wp_admin_bar/initialize/)`wp-includes/class-wp-admin-bar.php` |

Initializes the admin bar.

  | 
| [register_activation_hook()](https://developer.wordpress.org/reference/functions/register_activation_hook/)`wp-includes/plugin.php` |

Set the activation hook for a plugin.

  | 
| [register_deactivation_hook()](https://developer.wordpress.org/reference/functions/register_deactivation_hook/)`wp-includes/plugin.php` |

Sets the deactivation hook for a plugin.

  | 
| [WP_Customize_Setting::preview()](https://developer.wordpress.org/reference/classes/wp_customize_setting/preview/)`wp-includes/class-wp-customize-setting.php` |

Add filters to supply the setting’s value when accessed.

  | 
| [wp_enqueue_media()](https://developer.wordpress.org/reference/functions/wp_enqueue_media/)`wp-includes/media.php` |

Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs.

  | 
| [wp_playlist_scripts()](https://developer.wordpress.org/reference/functions/wp_playlist_scripts/)`wp-includes/media.php` |

Outputs and enqueues default scripts and styles for playlists.

  | 
| [WP_Rewrite::flush_rules()](https://developer.wordpress.org/reference/classes/wp_rewrite/flush_rules/)`wp-includes/class-wp-rewrite.php` |

Removes rewrite rules and then recreate rewrite rules.

  | 
| [add_feed()](https://developer.wordpress.org/reference/functions/add_feed/)`wp-includes/rewrite.php` |

Adds a new feed type like /atom1/.

  | 
| [WP_Scripts::__construct()](https://developer.wordpress.org/reference/classes/wp_scripts/__construct/)`wp-includes/class-wp-scripts.php` |

Constructor.

  | 
| [WP_Customize_Header_Image_Control::prepare_control()](https://developer.wordpress.org/reference/classes/wp_customize_header_image_control/prepare_control/)`wp-includes/customize/class-wp-customize-header-image-control.php` |

Prepares the control.

  | 
| [WP_Customize_Widgets::customize_preview_init()](https://developer.wordpress.org/reference/classes/wp_customize_widgets/customize_preview_init/)`wp-includes/class-wp-customize-widgets.php` |

Adds hooks for the Customizer preview.

  | 
| [WP_Customize_Widgets::__construct()](https://developer.wordpress.org/reference/classes/wp_customize_widgets/__construct/)`wp-includes/class-wp-customize-widgets.php` |

Initial loader.

  | 
| [WP_Customize_Widgets::schedule_customize_register()](https://developer.wordpress.org/reference/classes/wp_customize_widgets/schedule_customize_register/)`wp-includes/class-wp-customize-widgets.php` |

Ensures widgets are available for all types of previews.

  | 
| [wp_set_comment_status()](https://developer.wordpress.org/reference/functions/wp_set_comment_status/)`wp-includes/comment.php` |

Sets the status of a comment.

  | 
| [_WP_Editors::editor_settings()](https://developer.wordpress.org/reference/classes/_wp_editors/editor_settings/)`wp-includes/class-wp-editor.php` |  | 
| [wp_print_media_templates()](https://developer.wordpress.org/reference/functions/wp_print_media_templates/)`wp-includes/media-template.php` |

Prints the templates used in the media manager.

  |

[Show 85 more](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.2.0](https://developer.wordpress.org/reference/since/1.2.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 11 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-355)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-355)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-355)
     Vote results for this note: 46[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-355)
 4.  **Using with a Class**
      To use `add_action()` when your plugin or theme is built
     using classes, you need to use the array callable syntax. You would pass the function
     to `add_action()` as an array, with `$this` as the first element, then the name
     of the class method, like so:
 5.      ```php
         /**
          * Class WP_Docs_Class.
          */
         class WP_Docs_Class {
     
         	/**
         	 * Constructor
         	 */
         	public function __construct() {
         		add_action( 'save_post', array( $this, 'wpdocs_save_posts' ) );
         	}
     
         	/**
         	 * Handle saving post data.
         	 */
         	public function wpdocs_save_posts() {
         		// do stuff here...
         	}
         }
     
         $wpdocsclass = new WP_Docs_Class();
         ```
     
 6.   * Don’t use this in themes you want to distribute. It won’t be possible to remove
        the action as $this would be a different value;
      * [theking2](https://profiles.wordpress.org/theking2/) [4 years ago](https://developer.wordpress.org/reference/functions/add_action/#comment-6274)
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D355%23feedback-editor-355)
 8.   [Skip to note 12 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-356)
 9.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-356)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-356)
     Vote results for this note: 25[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-356)
 11. **Using with static functions in a class**
      If the class is called staticly the
     approach has to be like below as `$this` is not available. This also works if 
     class is extended. Use the following:
 12.     ```php
         /**
          * Class WP_Docs_Static_Class.
          */
         class WP_Docs_Static_Class {
     
         	/**
         	 * Initializer for setting up action handler
         	 */
         	public static function init() {
         		add_action( 'save_post', array( get_called_class(), 'wpdocs_save_posts' ) );
         	}
     
         	/**
         	 * Handler for saving post data.
         	 */
         	public static function wpdocs_save_posts() {
         		// do stuff here...
         	}
         }
     
         WP_Docs_Static_Class::init();
         ```
     
 13.  * If we have callback function which is static in another class, we can use the
        following: `add_action( 'save_post', array( Classname::class, 'wpdocs_save_posts'));`
      * [sumeetboga95](https://profiles.wordpress.org/sumeetboga95/) [4 years ago](https://developer.wordpress.org/reference/functions/add_action/#comment-6256)
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D356%23feedback-editor-356)
 15.  [Skip to note 13 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-3546)
 16.   [mkormendy](https://profiles.wordpress.org/mkormendy/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-3546)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-3546)
     Vote results for this note: 10[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-3546)
 18. To pass a variable to the called function of the action, you can use closures (
     since PHP 5.3+) when the argument is not available in the original coded do_action.
     For example:
 19.     ```php
         add_action('wp_footer', function($arguments) use ($myvar) { 
             echo $myvar;
         }, $priority_integer, $accepted_arguments_integer);
         ```
     
 20.  * Don’t use anonymous closures on themes you want to distribute. It would be 
        impossible to remove the action. Instead use closure variables: ` $myvar = '
        Hello World'; $action_wp_footer = function($arguments) use ($myvar) { echo 
        $myvar; }; add_action('wp_footer', $action_wp_footer, 100, 1); ` which can 
        be removed with: ` remove_action('wp_footer', $action_wp_footer, 100); ` with
        the advantage that typos in the closure name will yeild a helpfull Undfined
        variable error on typos.
      * [theking2](https://profiles.wordpress.org/theking2/) [4 years ago](https://developer.wordpress.org/reference/functions/add_action/#comment-6275)
 21.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D3546%23feedback-editor-3546)
 22.  [Skip to note 14 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-353)
 23.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-353)
 24. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-353)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-353)
 25. **Simple Hook**
      To email some friends whenever an entry is posted on your blog:
 26.     ```php
         /**
          * Send email to my friends.
          *
          * @param int $post_id Post ID.
          * @return int Post ID.
          */
         function wpdocs_email_friends( $post_id ) {
         	$friends = 'bob@example.org, susie@example.org';
         	wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );
     
         	return $post_id;
         }
         add_action( 'publish_post', 'wpdocs_email_friends' );
         ```
     
 27.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D353%23feedback-editor-353)
 28.  [Skip to note 15 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-2352)
 29.   [Anthony Hortin](https://profiles.wordpress.org/ahortin/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-2352)
 30. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-2352)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-2352)
 31. Related:
 32. [do_action()](https://developer.wordpress.org/reference/functions/do_action/)
     
     [remove_action()](https://developer.wordpress.org/reference/functions/remove_action/)
 33.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D2352%23feedback-editor-2352)
 34.  [Skip to note 16 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-6440)
 35.   [Bartek](https://profiles.wordpress.org/bartj/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-6440)
 36. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-6440)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-6440)
 37. I urge you, don’t attach your hook callbacks inside class’ constructor.
 38. Instead of implementing _official_ example most upvoted in this thread, opt for
     decoupled solution. You have one more line of code to write, but objects become
     more reusable and less error-prone (consider, what would happen if you call `new
     WP_Docs_Class()` twice in your code, following Codex example).
 39.     ```php
         /**
          * Class WP_Docs_Class.
          */
         class WP_Docs_Class {
     
         	/**
         	 * Initiate all hooks' callbacks in a separate method.
         	 */
         	public function hooks() {
         		add_action( 'save_post', array( $this, 'wpdocs_save_posts' ) );
         	}
     
         	/**
         	 * Handle saving post data.
         	 */
         	public function wpdocs_save_posts() {
         		// do stuff here...
         	}
         }
     
         $wpdocsclass = new WP_Docs_Class();
         $wpdocsclass->hooks();
         ```
     
 40.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D6440%23feedback-editor-6440)
 41.  [Skip to note 17 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-2658)
 42.   [lucasbustamante](https://profiles.wordpress.org/lucasbustamante/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-2658)
 43. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-2658)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-2658)
 44. **Passing parameters while using in a Class**
      To pass parameters to your method
     in a Class while calling it with add_action, you can do as following:
 45.     ```php
         public function __construct() {
             // Actions
             add_action('init', array($this, 'call_somefunction'));
         }
     
         /**
          *    Intermediate function to call add_action with parameters
          */
         public function call_somefunction() {
             $this->somefunction('Hello World');
         }
     
         /**
          *    Actual function that does something
          */
         public function somefunction($text) {
             echo $text;
         }
         ```
     
 46.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D2658%23feedback-editor-2658)
 47.  [Skip to note 18 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-3937)
 48.   [Christian Saborio](https://profiles.wordpress.org/christian-saborio/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-3937)
 49. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-3937)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-3937)
 50. How to add an action that calls a function (with parameters) from an instantiated
     class:
 51.     ```php
         $admin_menu_hider = new AdminMenuHider( UserManagement::get_internal_users() );
         			add_action(
         				'wp_before_admin_bar_render',
         				function () use ( $admin_menu_hider ) {
         					$admin_menu_hider->change_greeting_message( 'Hello' );
         				}
         			);
         ```
     
 52.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D3937%23feedback-editor-3937)
 53.  [Skip to note 19 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-354)
 54.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-354)
 55. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-354)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-354)
 56. **Accepted Arguments**
      A hooked function can optionally accept arguments from
     the action call, if any are set to be passed. In this simplistic example, the `
     echo_comment_id` function takes the `$comment_id` argument, which is automatically
     passed to when the `do_action()` call using the `comment_id_not_found` filter 
     hook is run.
 57.     ```php
         /**
          * Warn about comment not found
          *
          * @param int $comment_id Comment ID.
          */
         function echo_comment_id( $comment_id ) {
         	printf( 'Comment ID %s could not be found', esc_html( $comment_id ) );
         }
         add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );
         ```
     
 58.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D354%23feedback-editor-354)
 59.  [Skip to note 20 content](https://developer.wordpress.org/reference/functions/add_action/?output_format=md#comment-content-6158)
 60.   [theking2](https://profiles.wordpress.org/theking2/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/add_action/#comment-6158)
 61. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-6158)
     Vote results for this note: -4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%23comment-6158)
 62. To prevent runtime errors due to easy typo errors (defensive programming) and 
     prevent pollution of the global namespace use closures instead of function names.
     The sample code adjusted:
 63.     ```php
         /**
          * add a save post hook
          */
         add_action( 'save_post', function( $post_ID, $post, $update ) {
            // do stuff here
         }, 10, 3 );
         ```
     
 64.  * Do not do this in themes or plugins you intend to distribute, as it will prevent
        others from removing your callables if desired.
      * [crstauf](https://profiles.wordpress.org/crstauf/) [4 years ago](https://developer.wordpress.org/reference/functions/add_action/#comment-6165)
      * Not recommended, as you would be missing a valuable callback function name 
        in any stack trace.
      * [Dave Green](https://profiles.wordpress.org/davegreenwp/) [4 years ago](https://developer.wordpress.org/reference/functions/add_action/#comment-6348)
 65.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F%3Freplytocom%3D6158%23feedback-editor-6158)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_action%2F)
before being able to contribute a note or feedback.