Download the PHP package alleyinteractive/wp-captain-hook without Composer
On this page you can find all versions of the php package alleyinteractive/wp-captain-hook. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alleyinteractive/wp-captain-hook
More information about alleyinteractive/wp-captain-hook
Files in alleyinteractive/wp-captain-hook
Package wp-captain-hook
Short Description Tools for manipulating private action/filter callbacks in WordPress.
License GPL-2.0-or-later
Informations about the package wp-captain-hook
Captain Hook
Captain Hook is a library for WordPress developers to help them manipulate objects and methods hooked into actions and filters using otherwise-inaccessible object instances.
About
Oftentimes, a plugin will create an instance of an object in a function scope, or otherwise simply outside of the global scope, and then use that instance to hook methods into actions or filters. Here's an example:
If a developer wanted to unhook or otherwise alter the init
method from the init
action, they would need to have access to the $plugin
instance. This is not possible from outside the scope of the anonymous function that created the instance.
Captain Hook provides a way for developers to access and manipulate these objects and methods, even when they are not accessible from the global scope. Features of this library include:
- Remove Action or Filter by Force: Remove an action or filter from a hook.
- Reprioritize Actions and Filters: Change the priority of an action or filter method.
- Retrieve an object instance: Get the otherwise-inaccessible object instance for a method hooked into an action or filter.
Installation
Install the latest version with:
Basic usage
Remove Action or Filter by Force
Reprioritize Actions and Filters
Retrieve an object instance
Here's a fictituous example using the above plugin code. That code uses dependency injection to pass a file-based logger to the Plugin_Main
class. Say we want to log to Redis instead of a file, it looks like we should be able to replace the dependency using the set_logger()
method -- however, we can't do that because we don't have access to the instance of Plugin_Main
. Below, we leverage get_hooked_object
to retrieve the plugin instance and then call the set_logger
method on it to replace the file logger with a redis logger.