WordPress do_action() Function

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

Definition

In WordPress, do_action() fires an action hook. It means: “This moment has arrived. Anyone who is listening, run now.”

When it runs, WordPress finds every function added to that hook with add_action() and calls them in priority order. WordPress core uses do_action() for hooks like init and wp_head. You can add your own in a theme or plugin, so other code can add content or run tasks at that spot.

Syntax

Give it the hook name. Add extra values after it if the listeners need them.

WordPress
<?php do_action( 'my_custom_hook' ); ?>
CallWhat it does
do_action( 'my_hook' )Fire the hook with no extra data.
do_action( 'my_hook', $user_name )Fire the hook and pass one value to each function.
do_action( 'my_hook', $id, $total )Pass two values. Listeners ask for them with $accepted_args.

It is safe to call do_action() when nothing is hooked. In that case, nothing happens.

Full declaration
WordPress
do_action( string $hook_name, mixed ...$arg ): void

Parameters

ParameterDescription
$hook_namestring. The name of the action hook to fire. For your own hooks, use a unique prefix such as ctf_before_footer.
...$argmixed. Optional values to send to every callback. Each callback receives them in the same order.

Return Value

Return ValueDescription
voidNothing is returned. Each callback runs and any value it returns is ignored.

WordPress Examples

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

Example 1 — Make a custom hook in a template

Place a hook in your template. Other code can add content at that spot.

WordPress
<!-- footer.php -->
<?php do_action( 'ctf_before_footer' ); ?>
<footer>...</footer>

<?php
// functions.php
function ctf_newsletter_promo() {
  echo '<p class="promo">Join our newsletter!</p>';
}
add_action( 'ctf_before_footer', 'ctf_newsletter_promo' );

How It Works

  1. do_action( 'ctf_before_footer' ) marks a spot in the template.
  2. add_action() in functions.php attaches ctf_newsletter_promo() to that spot.
  3. When the page loads, the hook fires and the promo prints just above the footer.

Example 2 — Pass a value to the callbacks

Add data after the hook name. Every listener can use it.

WordPress
function ctf_welcome_message( $name ) {
  echo 'Welcome, ' . esc_html( $name ) . '!';
}
add_action( 'ctf_user_welcome', 'ctf_welcome_message' );

do_action( 'ctf_user_welcome', 'Asha' );

How It Works

  1. do_action() sends the text Asha with the hook.
  2. The callback receives it as $name. The default $accepted_args of 1 is enough.
  3. esc_html() keeps the output safe.

Example 3 — Run many functions in order

One hook can start several tasks. Priority sets the order, and two values are passed.

WordPress
function ctf_log_order( $order_id, $total ) {
  echo 'Order ' . $order_id . ' saved.' . "\n";
}
function ctf_email_order( $order_id, $total ) {
  echo 'Email sent. Total: $' . number_format( $total, 2 ) . "\n";
}
add_action( 'ctf_order_done', 'ctf_log_order', 10, 2 );
add_action( 'ctf_order_done', 'ctf_email_order', 20, 2 );

do_action( 'ctf_order_done', 101, 49.5 );

How It Works

  1. Two functions listen to ctf_order_done, with priority 10 and 20.
  2. do_action() sends two values: the order ID and the total.
  3. The priority 10 function runs first, then priority 20. Both asked for 2 values with 10, 2 and 20, 2.

Frequently Asked Questions

It fires an action hook. WordPress runs every function that was added to that hook with add_action().
do_action() triggers the hook at a point in the code. add_action() chooses which function runs when that trigger happens.
No. It returns nothing. If you need a changed value back, use apply_filters().
Yes. Call do_action() with a unique name in your theme or plugin, and others can use add_action() on it.
Yes. Add values after the hook name. Each listener must ask for them with the $accepted_args value in add_action().

Next: remove_action()

Unhook a function that was added with add_action().

remove_action() 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.

10 people found this page helpful