How to check if an action is already fired in a function for example:

function foo() {
  // checking if the action 'wp' is fired
  if( !action_fired('wp') ) {
    trigger_error('invalid call');
    return;
  }

  if( is_front_page() ) {
    // Do something
  }
}

I want to check that because:

  1. WordPress conditional tags like is_front_page won’t work being called before the ‘wp’ hook.

  2. Still, these conditional tags can be called before ‘wp’ without any warning or notices

So writing a function like foo(), I want to ensure it is called properly by throwing an error if the ‘wp’ action hasn’t fired at that time.

1 Answer
1

How to check if an action is already fired?

The did_action( $tag) function returns the number of times the $tag action hook has fired.

Leave a Reply

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