shinepress / hooks
Tools for managing WordPress hooks before and after initialization
1.0.0
2025-05-15 17:03 UTC
Requires
- php: >=8.1
Requires (Dev)
- php-cs-fixer/shim: ^3.0
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.0
- roots/wordpress-no-content: ^6.3
- shinepress/coding-standard: dev-main
- shinepress/framework: ^1.0
Suggests
- shinepress/framework: Allows use of attribute based hook registration.
This package is auto-updated.
Last update: 2025-05-15 17:04:47 UTC
README
Description
A tool for managing WordPress hooks. Allows registration of hooks prior to WordPress initialization as well as providing attributes that can be used for the same purpose in conjuction with the ShinePress framework.
Installation
The recommendend installation method is with composer:
$ composer require shinepress/hooks
Usage
HookManager
The HookManager class provides static methods for managing WordPress hooks.
use ShinePress\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
When using the framework, hooks can be defined by adding attributes to functions in your custom module class.
use ShinePress\Framework\Module; use ShinePress\Hooks\Filter; class CustomModule extends Module { #[Filter('lowercase')] public function lowercase(string $value): string { return strtolower($value); } #[Filter('uppercase')] public function uppercase(string $value): string { return strtoupper($value); } } CustomModule::register(); // WordPress Equivalent: // add_filter('lowercase', [CustomModule::instance(), 'lowercase'], 10, 1); // add_filter('uppercase', [CustomModule::instance(), 'uppercase'], 10, 1);
Multiple hooks can also be applied to a single callback.
use ShinePress\Framework\Module; use ShinePress\Hooks\Action; use ShinePress\Hooks\Filter; class CustomModule extends Module { #[Filter('example-filter', 12)] #[Action('example-action')] public function exampleFunction(mixed $value) { // do something } } CustomModule::register(); // WordPress Equivalent: // add_filter('example-filter', [CustomModule::instance(), 'exampleFunction'], 12, 1); // add_action('example-action', [CustomModule::instance(), 'exampleFunction'], 10, 1);