Hooks
The@wordpress/hooks package is a lightweight and efficient EventManager for JavaScript, providing a hooks system similar to WordPress PHP hooks. It allows you to add actions and filters to extend and modify behavior in your applications.
Installation
You can install the package using npm:This package assumes your code will run in an ES2015+ environment. If you’re targeting environments with limited support for modern JavaScript features, include the polyfill from
@wordpress/babel-preset-default.Basic Usage
Creating a Custom Hooks Instance
You can create your own isolated hooks instance:Using the Global Instance
The package provides a default global instance accessible through named exports:Key Differences from PHP Hooks
One notable difference from the PHP hooks API is that JavaScript hooks require a namespace as the second argument. The namespace uniquely identifies a callback in the formvendor/plugin/function.
Actions API
addAction( hookName, namespace, callback, priority )
Adds an action callback to be executed when the action is triggered.The name of the action hook. Must be a non-empty string containing only numbers, letters, dashes, periods, and underscores. Cannot begin with
__.A unique namespace for the callback in the form
vendor/plugin/function. Must be a non-empty string containing only numbers, letters, dashes, periods, underscores, and slashes.The function to be executed when the action is triggered.
The priority at which the callback should be executed. Lower numbers execute earlier.
doAction( hookName, …args )
Executes all callbacks registered for the given action hook synchronously.The name of the action hook to execute.
Arguments to pass to the callback functions.
doActionAsync( hookName, …args )
Executes all callbacks registered for the given action hook asynchronously.The name of the action hook to execute.
Arguments to pass to the callback functions.
A Promise that resolves when all callbacks have been executed.
removeAction( hookName, namespace )
Removes the callback registered for the given action hook and namespace.The name of the action hook.
The namespace of the callback to remove.
removeAllActions( hookName )
Removes all callbacks registered for the given action hook.The name of the action hook.
hasAction( hookName, namespace )
Checks if any callbacks are registered for the given action hook.The name of the action hook.
Optional namespace to check for a specific callback.
If namespace is provided, returns the priority of that callback or
false if not found. Otherwise, returns the number of callbacks registered.didAction( hookName )
Returns the number of times an action has been executed.The name of the action hook.
The number of times the action has been executed.
doingAction( hookName )
Checks if an action is currently being executed.Optional name of the action hook. If omitted, checks if any action is currently executing.
Whether the action is currently being executed.
Filters API
addFilter( hookName, namespace, callback, priority )
Adds a filter callback to modify a value when the filter is applied.The name of the filter hook.
A unique namespace for the callback in the form
vendor/plugin/function.The function to be executed. Must return a value.
The priority at which the callback should be executed.
applyFilters( hookName, value, …args )
Applies all callbacks registered for the given filter hook synchronously.The name of the filter hook to apply.
The value to filter.
Additional arguments to pass to the callback functions.
The filtered value.
applyFiltersAsync( hookName, value, …args )
Applies all callbacks registered for the given filter hook asynchronously.The name of the filter hook to apply.
The value to filter.
Additional arguments to pass to the callback functions.
A Promise that resolves to the filtered value.
removeFilter( hookName, namespace )
Removes the callback registered for the given filter hook and namespace.The name of the filter hook.
The namespace of the callback to remove.
removeAllFilters( hookName )
Removes all callbacks registered for the given filter hook.The name of the filter hook.
hasFilter( hookName, namespace )
Checks if any callbacks are registered for the given filter hook.The name of the filter hook.
Optional namespace to check for a specific callback.
If namespace is provided, returns the priority of that callback or
false if not found. Otherwise, returns the number of callbacks registered.didFilter( hookName )
Returns the number of times a filter has been applied.The name of the filter hook.
The number of times the filter has been applied.
doingFilter( hookName )
Checks if a filter is currently being applied.Optional name of the filter hook. If omitted, checks if any filter is currently being applied.
Whether the filter is currently being applied.
Advanced Features
createHooks()
Creates a new isolated hooks instance.A new Hooks instance with all the hooks methods.
Hook Events
Whenever an action or filter is added or removed, a matching event is triggered:- hookAdded: Triggered when
addFilter()oraddAction()is called, passing values forhookName,functionName,callback, andpriority. - hookRemoved: Triggered when
removeFilter()orremoveAction()is called, passing values forhookNameandfunctionName.