API Reference

HookManager

WPZylos\Framework\Hooks\HookManager

Manages WordPress actions and filters with a dual API: wp* methods for WordPress core hooks (never prefixed) and plain methods for custom plugin hooks (always prefixed).

Constructor

public function __construct(ContextInterface $context)
ParameterTypeDescription
$contextContextInterfacePlugin context used for hook prefixing

WordPress Core Hooks

These methods operate on WordPress hooks exactly as named. No prefixing is applied.

wpAction

public function wpAction(
    string $hook,
    callable $callback,
    int $priority = 10,
    int $acceptedArgs = 1
): static

Add an action listener to a WordPress core hook. Calls add_action() internally.

ParameterTypeDefaultDescription
$hookstring-WordPress hook name (e.g., 'init', 'admin_menu')
$callbackcallable-Callback function
$priorityint10Hook priority
$acceptedArgsint1Number of arguments passed

Returns: static (fluent)


wpFilter

public function wpFilter(
    string $hook,
    callable $callback,
    int $priority = 10,
    int $acceptedArgs = 1
): static

Add a filter listener to a WordPress core hook. Calls add_filter() internally.

ParameterTypeDefaultDescription
$hookstring-WordPress hook name (e.g., 'the_content', 'body_class')
$callbackcallable-Callback function
$priorityint10Hook priority
$acceptedArgsint1Number of arguments passed

Returns: static (fluent)


removeWpAction

public function removeWpAction(
    string $hook,
    callable $callback,
    int $priority = 10
): bool

Remove an action from a WordPress core hook. Calls remove_action() internally.

ParameterTypeDefaultDescription
$hookstring-WordPress hook name
$callbackcallable-Callback to remove
$priorityint10Priority used when adding

Returns: bool - true if successfully removed.


removeWpFilter

public function removeWpFilter(
    string $hook,
    callable $callback,
    int $priority = 10
): bool

Remove a filter from a WordPress core hook. Calls remove_filter() internally.

ParameterTypeDefaultDescription
$hookstring-WordPress hook name
$callbackcallable-Callback to remove
$priorityint10Priority used when adding

Returns: bool - true if successfully removed.


Custom Plugin Hooks

These methods automatically prefix the hook name using $context->hook() to create plugin-scoped hooks.

action

public function action(
    string $hook,
    callable $callback,
    int $priority = 10,
    int $acceptedArgs = 1
): static

Register a listener on a custom prefixed action hook. Calls add_action() with the prefixed hook name.

ParameterTypeDefaultDescription
$hookstring-Custom hook name without prefix
$callbackcallable-Callback function
$priorityint10Hook priority
$acceptedArgsint1Number of arguments passed

Returns: static (fluent)


filter

public function filter(
    string $hook,
    callable $callback,
    int $priority = 10,
    int $acceptedArgs = 1
): static

Register a listener on a custom prefixed filter hook. Calls add_filter() with the prefixed hook name.

ParameterTypeDefaultDescription
$hookstring-Custom hook name without prefix
$callbackcallable-Callback function
$priorityint10Hook priority
$acceptedArgsint1Number of arguments passed

Returns: static (fluent)


doAction

public function doAction(string $hook, mixed ...$args): void

Fire a custom prefixed action hook. Calls do_action() with the prefixed hook name.

ParameterTypeDescription
$hookstringCustom hook name without prefix
...$argsmixedArguments to pass to callbacks

Returns: void


applyFilter

public function applyFilter(string $hook, mixed $value, mixed ...$args): mixed

Apply a custom prefixed filter hook. Calls apply_filters() with the prefixed hook name.

ParameterTypeDescription
$hookstringCustom hook name without prefix
$valuemixedValue to filter
...$argsmixedAdditional arguments

Returns: mixed - The filtered value.

Note: This method is applyFilter (singular), not applyFilters.


removeAction

public function removeAction(
    string $hook,
    callable $callback,
    int $priority = 10
): bool

Remove a listener from a custom prefixed action hook.

ParameterTypeDefaultDescription
$hookstring-Custom hook name without prefix
$callbackcallable-Callback to remove
$priorityint10Priority used when adding

Returns: bool - true if successfully removed.


removeFilter

public function removeFilter(
    string $hook,
    callable $callback,
    int $priority = 10
): bool

Remove a listener from a custom prefixed filter hook.

ParameterTypeDefaultDescription
$hookstring-Custom hook name without prefix
$callbackcallable-Callback to remove
$priorityint10Priority used when adding

Returns: bool - true if successfully removed.


One-Time Hooks

once

public function once(
    string $hook,
    callable $callback,
    int $priority = 10
): static

Add a one-time action that removes itself after first execution. The hook name is not prefixed (uses wpAction() internally).

ParameterTypeDefaultDescription
$hookstring-WordPress hook name
$callbackcallable-Callback function
$priorityint10Hook priority

Returns: static (fluent)


Registry Access

getRegisteredActions

public function getRegisteredActions(): array

Get all actions registered through this HookManager instance.

Returns: array - Keyed by hook name, each containing arrays of ['callable' => ..., 'priority' => ...].


getRegisteredFilters

public function getRegisteredFilters(): array

Get all filters registered through this HookManager instance.

Returns: array - Keyed by hook name, each containing arrays of ['callable' => ..., 'priority' => ...].


hasHook

public function hasHook(string $hook): bool

Check if any hooks (actions or filters) are registered for the given hook name.

ParameterTypeDescription
$hookstringHook name (exact, including prefix if custom)

Returns: bool


prefix

public function prefix(string $hook): string

Get the prefixed version of a custom hook name.

ParameterTypeDescription
$hookstringHook name without prefix

Returns: string - The prefixed hook name.


HookServiceProvider

WPZylos\Framework\Hooks\HookServiceProvider

Registers the HookManager with the application container.

register

public function register(ApplicationInterface $app): void

Registers two container bindings:

BindingTypeDescription
HookManager::classSingletonHookManager instance
'hooks'SingletonAlias to HookManager

The HookManager receives $app->context() as its ContextInterface dependency.