https://github.com/yardinternet/wp-hook-registrar
An Acorn package for WordPress Hook Registration.
https://github.com/yardinternet/wp-hook-registrar
acorn wordpress
Last synced: about 1 year ago
JSON representation
An Acorn package for WordPress Hook Registration.
- Host: GitHub
- URL: https://github.com/yardinternet/wp-hook-registrar
- Owner: yardinternet
- License: mit
- Created: 2024-11-06T13:28:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-31T10:36:01.000Z (about 1 year ago)
- Last Synced: 2025-03-31T11:31:40.980Z (about 1 year ago)
- Topics: acorn, wordpress
- Language: PHP
- Homepage:
- Size: 441 KB
- Stars: 6
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wp-hook-registrar
[](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/format-php.yml)
[](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/phpstan.yml)
[](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/run-tests.yml)
[](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/badges.yml)
[](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/badges.yml)
An Acorn package for WordPress Hook Registration.
## Features
- [x] Register Hooks using php attributes
- [x] Configure Hook registration using a config file
- [x] Load plugin-specific hooks only when the plugin is active
See [config](./config/hooks.php) for all configuration options.
## Requirements
- [Sage](https://github.com/roots/sage) >= 10.0
- [Acorn](https://github.com/roots/acorn) >= 4.0
## Installation
1. Install this package with Composer:
```sh
composer require yard/wp-hook-registrar
```
2. Run the Acorn WP-CLI command to discover this package:
```shell
wp acorn package:discover
```
3. Publish the config file with:
```shell
wp acorn vendor:publish --provider="Yard\Hook\HookServiceProvider"
```
4. Register all your project hooks in the published configuration file `config/hooks.php`.
## Installation in WordPress plugins
To use this package in a standard WordPress plugin, you can use the `HookRegistrar` to register hooks.
You can skip step 3 and 4 from the installation instructions above and instead add the following to your plugin's
main file:
```php
/**
* Plugin Name: My Plugin
*/
require __DIR__ . '/vendor/autoload.php';
$classNames = [
\Plugin\ClassContainsHooks::class,
\Plugin\AnotherClassContainsHooks::class,
];
$registrar = new \Yard\Hook\Registrar($classNames);
$registrar->registerHooks();
```
## Hook Attributes
This package provides two Attributes: `Action` and `Filter`. They can be used to register hooks instead of the
WordPress functions [add_action()](https://developer.wordpress.org/reference/functions/add_action/) and [add_filter()](https://developer.wordpress.org/reference/functions/add_filter/)
This syntax allows you to place the hook registration directly above the method it invokes when the hook is triggered.
```php
#[Action(string $hookName, int $priority = 10)]
public function doSomething(): void
```
```php
#[Filter(string $hookName, int $priority = 10)]
public function filterSomething(): mixed
```
Notice that you do not need to pass the number of accepted arguments to the `Action` and `Filter` attributes as you would
with `add_action()` and `add_filter()`. Instead, the number of accepted arguments is determined by the method
signature.
You can add as many hooks to the same method as you want.
## Example
```php