Home WordPress Hooks Functions do_action() WordPress do_action() Function 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.
<?php do_action( 'my_custom_hook' ); ?> Call What 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 do_action( string $hook_name, mixed ...$arg ): void Parameters Parameter Description $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 Value Description void Nothing 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.
1. Make a custom hook in a template 2. Pass a value to the callbacks 3. Run many functions in order Example 1 — Make a custom hook in a template Place a hook in your template. Other code can add content at that spot.
<!-- 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' ); <p class="promo">Join our newsletter!</p>
<footer>...</footer> How It Works do_action( 'ctf_before_footer' ) marks a spot in the template.add_action() in functions.php attaches ctf_newsletter_promo() to that spot.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.
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 do_action() sends the text Asha with the hook.The callback receives it as $name. The default $accepted_args of 1 is enough. 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.
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 ); Order 101 saved.
Email sent. Total: $49.50 How It Works Two functions listen to ctf_order_done, with priority 10 and 20. do_action() sends two values: the order ID and the total.The priority 10 function runs first, then priority 20. Both asked for 2 values with 10, 2 and 20, 2. Frequently Asked Questions What does do_action() do? It fires an action hook. WordPress runs every function that was added to that hook with add_action().
What is the difference between do_action() and add_action()? do_action() triggers the hook at a point in the code. add_action() chooses which function runs when that trigger happens.
Does do_action() return anything? No. It returns nothing. If you need a changed value back, use apply_filters().
Can I create my own action hooks? Yes. Call do_action() with a unique name in your theme or plugin, and others can use add_action() on it.
Can I pass data to the functions? Yes. Add values after the hook name. Each listener must ask for them with the $accepted_args value in add_action().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion