WordPress add_action() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Action hook

Definition

In WordPress, add_action() says: “When this event happens, run my function.” The event is called a hook, and a hook that lets you do something is called an action.

WordPress marks many moments in a page request, such as loading styles, printing the head, or saving a post. At each moment it calls do_action(). Your function runs only when that happens. A function you add with add_action() is called a callback, and its return value is ignored.

Syntax

Give it the hook name and the name of your function.

WordPress
<?php add_action( 'init', 'my_function' ); ?>
CallWhat it does
add_action( 'wp_enqueue_scripts', 'my_styles' )Run my_styles() when WordPress loads CSS and JavaScript.
add_action( 'wp_head', 'my_meta', 5 )Run my_meta() early, because 5 is lower than the default 10.
add_action( 'save_post', 'my_save', 10, 3 )Let my_save() receive 3 values from the hook.

Put add_action() in functions.php or a plugin file, so it runs before the hook fires.

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

Parameters

ParameterDescription
$hook_namestring. The name of the action hook, for example init, wp_head, or wp_enqueue_scripts.
$callbackcallable. The function to run. Usually the name of a function as a string, such as 'my_function'.
$priorityint. Order of running. Lower numbers run earlier. Default is 10. Callbacks with the same priority run in the order you added them.
$accepted_argsint. How many values from the hook your function can receive. Default is 1.

Return Value

Return ValueDescription
trueAlways returns true. You do not need to check it.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Load a stylesheet

The most common use. Add your CSS file when WordPress prepares the page.

WordPress
function ctf_load_styles() {
  wp_enqueue_style( 'ctf-main', get_stylesheet_uri(), array(), '1.0' );
}
add_action( 'wp_enqueue_scripts', 'ctf_load_styles' );

How It Works

  1. ctf_load_styles() is a normal function that adds a stylesheet.
  2. add_action() tells WordPress to run it on the wp_enqueue_scripts hook.
  3. WordPress fires that hook while it builds the page, so the link tag appears in the head.

Example 2 — Control the order with priority

Two functions use the same hook. Priority decides which one runs first.

WordPress
function ctf_meta_author() {
  echo '<meta name="author" content="CodeToFun">' . "\n";
}
function ctf_meta_robots() {
  echo '<meta name="robots" content="index, follow">' . "\n";
}
add_action( 'wp_head', 'ctf_meta_author', 20 );
add_action( 'wp_head', 'ctf_meta_robots', 5 );

How It Works

  1. Both functions are attached to wp_head.
  2. Priority 5 is lower than 20, so ctf_meta_robots() runs first.
  3. The order you wrote the add_action() lines does not matter. Only the priority matters.

Example 3 — Receive values from the hook

Some hooks pass extra values. Use the fourth argument to say how many you want.

WordPress
function ctf_after_save( $post_id, $post, $update ) {
  if ( wp_is_post_revision( $post_id ) ) {
    return;
  }
  $label = $update ? 'updated' : 'created';
  error_log( 'Post ' . $post_id . ' was ' . $label . ': ' . $post->post_title );
}
add_action( 'save_post', 'ctf_after_save', 10, 3 );

How It Works

  1. The save_post hook passes 3 values: the post ID, the post object, and an update flag.
  2. 3 at the end of add_action() lets your function receive all three.
  3. Without it, only the first value ($post_id) would arrive. The output goes to debug.log when WP_DEBUG_LOG is on.

Frequently Asked Questions

It attaches your function to an action hook. When WordPress fires that hook with do_action(), your function runs.
A hook is a named point in the code where WordPress lets you run your own code (an action) or change a value (a filter).
No. Actions do something, such as printing HTML or saving data. The return value is ignored. Use add_filter() when you need to change a value.
Priority sets the order. Lower numbers run earlier, and the default is 10. Use it when two functions use the same hook.
Put it in your theme's functions.php or in a plugin file. It must run before WordPress fires the hook.

Next: add_filter()

Change a value when a WordPress filter hook runs.

add_filter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful