Advanced WordPress Hooks

When I develop WordPress Plugins or WordPress Themes, I like to always consider organization and efficiency. Organization comes in many forms. In this article, I’d like to discuss hook organization.

There are times where you might not want an event to occur until another event has occurred; Or maybe you want to have an event conditionally occur before or after another event has occurred.

Hooks Within Hooks

Think about your WordPress Plugin or WordPress Theme. What is being loaded and when? Why is it loaded there? Could it be loaded somewhere else or at a different time?

I’m a believer that everything in your WordPress Plugin or WordPress Theme should be loaded at a specific point in the “WordPress Execution Process”. You can think of the WordPress Execution Process as a series of events, which occur in chronological order from the beginning (when nothing has occurred) to the end (when everything has occurred). Each “event” that occurs is a “hook point” in the “execution process” .

Define a Hook Inside the Callback Function of Another Hook

Here’s a couple of  examples from one of my WordPress Plugins which I use to load most of the files needed on every page of my plugin.

Example of Hook within Hook using plugins_loaded

In the above example, you can see that my entire plugin is loaded on the core WordPress Hook plugins_loaded. Inside my callback function, I add two of my own hook points. One hook BEFORE my files are loaded, and another hook  AFTER my files are loaded. This provides the flexibility of allowing other WordPress Plugins and WordPress themes to execute code before or after my plugin has been loaded.

I’ll provide one more example. This is a common scenario in almost ANY WordPress Plugin using Custom Post Types or Custom Taxonomies.

Example of Hooks Within Hooks Using init

In the above example, my plugin is hooking into the core WordPress Hook init so that Custom Post Types and Custom Taxonomies can be registered.

The files which register Post Types and Taxonomies are actually loaded previously on plugins_loaded as illustrated in the previous code block example. My Custom Post Types and Custom Taxonomies are actually hooked into PLUGIN_SLUG . '-initialize-plugin. However, I just manually inserted them right BEFORE the hook event is triggered to demonstrate an illustration.

In this particular example, I’m able to hook into the core WordPress hook  of init so that my Custom Post Types and Custom Taxonomies can be registered. Now, I’m able to provide a way of before and after the Post Types and Taxonomies have been registered. Again, think of the flexibility this allows for other WordPress Plugins and WordPress Themes to interact with your plugin.

Hook to One Event and Then Hook to Another Event

There may come a time where you need access to information, but it’s not available until a certain point of the WordPress Execution Process. Then depending on the value of the information, you might conditionally hook into ANOTHER event. This is exactly what the two code block examples above demonstrate.

Example of Hooking Into One Event and Then Hooking to Another Event

In the example above, I am hooking into the WordPress core hook init and checking to see if my plugin settings are retrievable from the database. If the setting’s don’t exist, nothing more needs to be done. However, if there are plugin settings available, it will then hook into the core WordPress hook of template_redirect, which is where I would then handle content output.

Callback Functions Within Callback Functions

With WordPress hooks, you’re typically only limited to passing information which is available at the definition of the WordPress hook (whether this be an action hook or filter hook). It doesn’t mean that you CAN’T pass ADDITIONAL information to your callback function.

I’d like to demonstrate advanced callback functions, which go hand-in-hand with hooks within hooks. What I’m about to show you allows you to: hook into one event of the WordPress Execution Process, retrieve plugin data, conditionally hook into ANOTHER event, and finally, pass that plugin data along with the the additional event.

Example of Hook Within Hook and Callback Within Callback

Typically in the above example, if you wanted to filter the_content, you would be limited to only one variable and that would be the content you wish to filter. However, maybe you want to conditionally append additional content. This would be a way you could achieve that.

I’m not saying this is the most ideal and practical way. Although, there have been situations where this is the only way I’ve been able to pass the necessary information needed to a hooked callback function. As a general rule of thumb, I tend to advise against using anonymous functions.

I hope you found this article about WordPress hooks useful. Code is poetry.


  1. WordPress Hooks
  2. WordPress Action Hooks
  3. WordPress Filter Hooks
  4. PHP Namespaces
  5. FAQ: things you need to know about namespaces
  6. PHP Callback Functions
  7. PHP Anonymous Functions
  8. PHP Constants
  9. WordPress Hooks Database
  10. Advanced WordPress Hooks
  11. WordPress Quick Callback Function Returns

Leave a Reply