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)
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | WordPress hook name (e.g., 'init', 'admin_menu') |
$callback | callable | - | Callback function |
$priority | int | 10 | Hook priority |
$acceptedArgs | int | 1 | Number 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | WordPress hook name (e.g., 'the_content', 'body_class') |
$callback | callable | - | Callback function |
$priority | int | 10 | Hook priority |
$acceptedArgs | int | 1 | Number 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | WordPress hook name |
$callback | callable | - | Callback to remove |
$priority | int | 10 | Priority 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | WordPress hook name |
$callback | callable | - | Callback to remove |
$priority | int | 10 | Priority 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | Custom hook name without prefix |
$callback | callable | - | Callback function |
$priority | int | 10 | Hook priority |
$acceptedArgs | int | 1 | Number 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | Custom hook name without prefix |
$callback | callable | - | Callback function |
$priority | int | 10 | Hook priority |
$acceptedArgs | int | 1 | Number 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.
| Parameter | Type | Description |
|---|---|---|
$hook | string | Custom hook name without prefix |
...$args | mixed | Arguments 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.
| Parameter | Type | Description |
|---|---|---|
$hook | string | Custom hook name without prefix |
$value | mixed | Value to filter |
...$args | mixed | Additional arguments |
Returns: mixed - The filtered value.
Note: This method is
applyFilter(singular), notapplyFilters.
removeAction
public function removeAction(
string $hook,
callable $callback,
int $priority = 10
): bool
Remove a listener from a custom prefixed action hook.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | Custom hook name without prefix |
$callback | callable | - | Callback to remove |
$priority | int | 10 | Priority 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | Custom hook name without prefix |
$callback | callable | - | Callback to remove |
$priority | int | 10 | Priority 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).
| Parameter | Type | Default | Description |
|---|---|---|---|
$hook | string | - | WordPress hook name |
$callback | callable | - | Callback function |
$priority | int | 10 | Hook 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.
| Parameter | Type | Description |
|---|---|---|
$hook | string | Hook name (exact, including prefix if custom) |
Returns: bool
prefix
public function prefix(string $hook): string
Get the prefixed version of a custom hook name.
| Parameter | Type | Description |
|---|---|---|
$hook | string | Hook 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:
| Binding | Type | Description |
|---|---|---|
HookManager::class | Singleton | HookManager instance |
'hooks' | Singleton | Alias to HookManager |
The HookManager receives $app->context() as its ContextInterface dependency.