You can use did_action
to test for this, e.g.:
if ( did_action( 'init' ) < 1 ) {
// init has not ran yet! Throw the exception
}
https://developer.wordpress.org/reference/functions/did_action/
did_action
returns the number of times that action has been run. This can also be used to ensure it only happens once for filters that happen multiple times, e.g. using === 1
for the first time it runs
You can also use doing_action
to test if you are on the init
hook:
if ( ! doing_action( 'init' ) ) {
// only on init! throw an exception
}
https://developer.wordpress.org/reference/functions/doing_action/
You might also want to avoid exceptions, and instead consider using _doing_it_wrong
https://developer.wordpress.org/reference/functions/_doing_it_wrong/
This allows you to be more flexible while nudging the developer in the right direction for those situations where it needs to change, especially if this is a change in behaviour