I need to protect some hooks which only can be hooked by functions/callbacks within my theme/plugin.

For example:

if ( is_protected_hook('hook_name') ) {
  throw new \Exception('You cannot hook to a protected hook.');
} else {

Is there a way to define the is_protected_hook() function?

Any suggestions will be greatly appreciated!

2 Answers 2


As noted in my answer to your related question there is a datastructre $wp_filter that stores all information on hooks and filters. You may want to try a var_dump on it just to see what it looks like. There is no built in variable 'protected'.

This leaves you with two options to keep the administration of the hooks you want to protect: build it into $wp_filter yourself or keep it separate. I recommend the latter.

Maintain an array $protected_hooks. I don't know the conditions under which you want hooks to be protected, but you will have to set this array the moment you add an action to a specific hook.

Now, in your tempate file you will need a double condition: is a hook active and is it protected. That would go like this:

if ((has_filter('hook_name') && in_array('hook_name',$protected_hooks)) ...

This can not be solved by code, as wordpress, themes and plugins occupy the same data and program space, therefor there is no way to segregate anything in a way which will be bullet prof.

The question is why do you want to do such a thing at all? if it is because it is unstable, then you can just explicitly document next to the use of the hook that it is for internal usage only.

  • From the question I gather that this is about hooks that are native to a certain theme/plugin. That's a controlled environment in which you could pull of a trick like this.
    – cjbj
    Aug 31, 2016 at 15:03
  • @cjbj, unless you write on porpuse bad unreadable code, everyone can read your source code and find the functions that you use to implement your own hooks. Without blocking the call to that API as well, rewriting the hook handling code as you suggested in kinda pointless in the context of this question, just makes the code harder to understand but do not actually prevent its use Aug 31, 2016 at 16:03
  • Thank @MarkKaplun! It sounds defensive but I want the hook is 100% protected. ;-)
    – wpclevel
    Aug 31, 2016 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.