PHPackages                             yard/wp-hook-registrar - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. yard/wp-hook-registrar

ActivePackage

yard/wp-hook-registrar
======================

A package for WordPress hook registration.

v2.0.1(6mo ago)85.6k↓33.9%2MITPHPPHP &gt;=8.1CI passing

Since Nov 15Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/yardinternet/wp-hook-registrar)[ Packagist](https://packagist.org/packages/yard/wp-hook-registrar)[ RSS](/packages/yard-wp-hook-registrar/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (11)Used By (2)

wp-hook-registrar
=================

[](#wp-hook-registrar)

[![Code Style](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/format-php.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/format-php.yml)[![PHPStan](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/phpstan.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/phpstan.yml)[![Tests](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/run-tests.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/run-tests.yml)[![Code Coverage Badge](https://github.com/yardinternet/wp-hook-registrar/raw/badges/coverage.svg)](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/badges.yml)[![Lines of Code Badge](https://github.com/yardinternet/wp-hook-registrar/raw/badges/lines-of-code.svg)](https://github.com/yardinternet/wp-hook-registrar/actions/workflows/badges.yml)

Register Hooks using php attributes.

Installation
------------

[](#installation)

Install this package with Composer:

```
```sh
composer require yard/wp-hook-registrar
```

```

Usage
-----

[](#usage)

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:

```
/**
 * 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
---------------

[](#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.

```
#[Action(string $hookName, int $priority = 10)]
public function doSomething(): void
```

```
#[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
-------

[](#example)

```
