Some hooks get fired more than once, meaning that functions connected to those hooks are ran multiple times. This causes problems with some functions when they are designed to only be run once. If you have a function that is connected to an action hook, and you need to ensure it only ever runs once, then WordPress gives you a really nice, simple way of doing that. The did_action() function can be used to count the number of times an action has fired.

Take the function below as an example:

1
2
3
4
5
6
function pippin_sample_function() {
	if(did_action('my_custom_action') === 1) {
		// this code only runs the first time the "my_custom_action" hook is fired
	}
}
add_action('my_custom_action', 'pippin_sample_function'

“my_custom_action” is a custom hook that we have (presumably) setup elsewhere in our plugin, though any of the core WordPress hooks could be used as well. By using did_action(), we are able to conditionally run the code in the function only the first time the hook is fired. If the hook is fired a second, third, or any other number of times, the code within the IF statement will not be run. When a hook is fired, it’s did_action() count is increased then the functions connected to it are run. We know this by looking at the source for the do_action() function in wp-includes/plugin.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
function do_action($tag, $arg = '') {
	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
 
	if ( ! isset($wp_actions) )
		$wp_actions = array();
 
	if ( ! isset($wp_actions[$tag]) )
		$wp_actions[$tag] = 1;
	else
		++$wp_actions[$tag];
 
	// rest of the function has been omitted
}

Notice where $wp_actions[$tag] is incremented? That is the count used by the did_action() function.

  1. corsonr

    +1

    • Pippin

      Thanks!

  2. kathy

    i just needed something run once and ended up using some custom code i found on wordpress answers that created a new entry in the db. didn’t know there was a method in core i could’ve used . way cool. +1 indeed!

  3. chrismccoy

    didnt know about did_function ;) learn something new everyday with wp

    • Pippin

      I stumbled across it by chance. It’s a really cool little function, and even cooler when you figure out exactly how it works ;)

  4. A.Hariri

    Thanks for this useful post.

    I was wondering if I used something:
    if( is_user_logged_in() AND is_page(‘2661′) AND did_action(‘wp_footer’) === 1 )

    Would this run wp_footer for logged in users when they visit this page or did_action can’t be used in this way?

    What I am trying to do is run a code only on first visit for this page for each member in my blog. This is useful for event tracking for instance.

    Thanks!

    • Pippin

      Yep, that’s exactly how that would work.

  5. A.Hariri

    Thanks for confirming :). So, just to understand it correctly, when they visit the same page again later, the function will not work anymore on that page?

    Thanks!

    • Pippin

      No, it does not persist across page loads. It is for detecting if the action has fired during that particular page load.

    • A.Hariri

      Oh I see! Thanks for explaining that :). I guess I could create a meta-field and check it’s value for each user to run a function only once per user.

      Thanks again!

Your email address will not be published. Required fields are marked *