Hooks Service

The hooks service is first-of-all a single-handler service and second-of-all a runnable and resettable object. It's a service designed to work with WordPress actions and filters (commonly referred to as hooks). Basically the service provides the following public methods:

  • add_action - registers an action with the handler

  • remove_action - removes an action registered with the handler

  • remove_all_actions - removes all actions registered with the handler

  • add_filter - registers a filter with the handler

  • remove_filter - remove a filter registered with the handler

  • remove_all_filters - removes all filters registered with the handler

All of these methods are just wrappers against the registered handler. Any handler implementing the HooksHandlerInterfacearrow-up-right can be registered with the hooks service, but you may also decide to simply use the default handler instantiated if no handler is passed on in the constructor. The default handler is an instance of the DefaultHooksHandlerarrow-up-right inspired by the loaderarrow-up-right defined in the WordPress Plugin Boilerplate by DevinVinsonarrow-up-right.

circle-info

The intention is to only register the hooks with the WordPress system if the plugin was successfully initialized. If an error occurred, we don't want any half-hooks being called.

The default handler maintains all of the registered hooks in protected arrays and calls WordPress' own add_action and add_filter functions on its own run action.

circle-info

The handler's run and reset methods are automatically called when the hooks service's respective methods are called.

Bypassing the late registration

This approach arguably has its downsides. For example, you can't use any of your own hooks during plugin initialization (because that's when you're supposed to call run on the service). If you want to bypass this, there are 2 options available:

  1. Just don't use the service. We think the advantages outweigh the disadvantages, but that's for you to decide.

  2. Write your own custom handler implementing the HooksHandlerInterfacearrow-up-right interface and have it call the WP API directly.

Available Traits

There are 3 traits available for working with the hooks service.

First there is the HooksServiceAwareTraitarrow-up-right and the corresponding HooksServiceAwareInterfacearrow-up-right. Basically this allows you to call upon the hooks service instance from anywhere within the object. Technically, you can also use more than one service in your plugin and register different ones with different objects.

The second method involves injecting the hooks service from outside the instance. This behavior is modelled by the HooksServiceRegisterInterfacearrow-up-right interface and the HooksServiceRegisterTraitarrow-up-right.

The recommended way, however, is to use the SetupHooksTraitarrow-up-right action trait. It's an action extension trait for automagically calling the aforementioned register_hooks methods upon the setup action. If attempts to obtain an instance of the HooksService either from the object itself (if it implements the HooksServiceAwareInterface interface) or from a dependency injection container.

There is also an accompanying InitializeHooksServiceTraitarrow-up-right action trait. This one attempts to set the hooks service on the instance by first querying its parent and lastly the dependency injection container for an instance.

Putting it all together, your code could look something like this:

circle-info

If the number of imported interfaces and traits in the example above is scarring you, you can find some comfort in the fact that this is an extreme example built from scratch! Normally you would create pre-built abstract objects that implement most of these features and simply extend them. That's basically what our Core Module does!

Last updated