shineunited / wordpress-hooks
Tool for managing WordPress hooks prior to initialization.
Requires
- php: >=8.0
Requires (Dev)
- composer/composer: ^2.4
- phpcompatibility/php-compatibility: ^9.3
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- roots/wordpress-no-content: >=5.6
- shineunited/coding-standard: ^1.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-12-18 03:21:05 UTC
README
Description
A tool for managing WordPress hooks. Allows registration of hooks prior to WordPress initialization and provides a framework for defining hooks using PHP attributes.
Installation
to add wordpress-hooks, the recommended method is via composer.
$ composer require shineunited/wordpress-hooks
HookManager
The HookManager class provides static methods for managing WordPress hooks. While most of the functions are simply aliased to the built-in WordPress functions, some of them all management of hooks prior to initialization.
use ShineUnited\WordPress\Hooks\HookManager; function callback_function($param1, $param2) { // code } // this can be used prior to initialization HookManager::addFilter('filter-name', 'callback-function', 10, 2);
Attribute Hooks
Hooks can also be defined by using the Hook attributes and HookManager::register();
use ShineUnited\WordPress\Hooks\Filter; use ShineUnited\WordPress\Hooks\HookManager; class MyObjectHooks { #[Filter('lowercase')] public function lowercase(string $value): string { return strtolower($value); } #[Filter('uppercase')] public function uppercase(string $value): string { return strtoupper($value); } } $hooks = new MyObjectHooks(); HookManager::register($hooks); // WordPress Equivalent // add_filter('lowercase', [$hooks, 'lowercase'], 10, 1); // add_filter('uppercase', [$hooks, 'uppercase'], 10, 1);
Multiple hooks can be applied to a single callback.
use ShineUnited\WordPress\Hooks\Action; use ShineUnited\WordPress\Hooks\Filter; use ShineUnited\WordPress\Hooks\HookManager; $closure = #[Filter('example-filter', 12)] #[Action('example-action')] function($value) { // code }; HookManager::register($closure); // WordPress Equivalent: // add_filter('example-filter', $closure, 12, 1); // add_filter('example-action', $closure, 10, 1);
Function Reference
For more details and examples please see our documentation.