PHPackages                             syntatis/wp-hook - 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. [Search &amp; Filtering](/categories/search)
4. /
5. syntatis/wp-hook

Abandoned → [syntatis/codex](/?search=syntatis%2Fcodex)ArchivedLibrary[Search &amp; Filtering](/categories/search)

syntatis/wp-hook
================

WordPress hook with object-oriented programming

v0.4.1(1y ago)12.2kGPL-2.0-or-laterPHPPHP &gt;=7.4

Since Dec 30Pushed 1y agoCompare

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

READMEChangelogDependencies (11)Versions (11)Used By (0)

🪝 wp-hook
=========

[](#-wp-hook)

[![Packagist Dependency Version](https://camo.githubusercontent.com/6a88836a455d05cb4e39f3dccba98adeaecc35e39d3343c28d529df2780cd486/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f73796e74617469732f77702d686f6f6b2f7068703f636f6c6f723d253233374138364238)](https://camo.githubusercontent.com/6a88836a455d05cb4e39f3dccba98adeaecc35e39d3343c28d529df2780cd486/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f73796e74617469732f77702d686f6f6b2f7068703f636f6c6f723d253233374138364238) [![wp](https://github.com/syntatis/wp-hook/actions/workflows/wp.yml/badge.svg)](https://github.com/syntatis/wp-hook/actions/workflows/wp.yml) [![codecov](https://camo.githubusercontent.com/6e8147d3d1d70fb7de1eb98c362a9cc551e7b506e9826cb33f8dafaa34a5c7f0/68747470733a2f2f636f6465636f762e696f2f67682f73796e74617469732f77702d686f6f6b2f67726170682f62616467652e7376673f746f6b656e3d3034485a3342524d3139)](https://codecov.io/gh/syntatis/wp-hook)

Caution

This package is now part of [Codex](https://github.com/syntatis/codex).

A class wrapper designed to use WordPress hooks with object-oriented programming (OOP), inspired by the [WordPress Plugin Boilerplate](https://wppb.me/). This class helps you stay organized and maintain structure when managing hooks in your WordPress plugin or theme.

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

[](#installation)

Install the package via Composer:

```
composer require syntatis/wp-hook
```

Usage
-----

[](#usage)

Create a new instance of the `Registry` class and register your hooks:

```
use Syntatis\WPHook\Registry;

$registry = new Registry();
$registry->addAction('init', 'initialise');
$registry->addFilter('the_content', 'content', 100);
$registry->addAction('add_option', 'option', 100, 2);
```

### Using PHP Attributes

[](#using-php-attributes)

If your theme or plugin runs on PHP 8.0 or later, you can leverage [PHP Attributes](https://www.php.net/manual/en/language.attributes.overview.php) to define hooks directly within your classes:

```
use Syntatis\WPHook\Action;
use Syntatis\WPHook\Filter;
use Syntatis\WPHook\Registry;

#[Action(name: "wp")]
class HelloWorld
{
    #[Action(name: "init")]
    public function initialise(): void
    {
        echo 'initialise';
    }

    #[Filter(name: "the_content", priority: 100)]
    public function content(string $content): string
    {
        return $content . "\ncontent";
    }

    #[Action(name: "the_content", priority: 100, acceptedArgs: 2)]
    public function option(string $optionName, mixed $value): void
    {
        echo $optionName . $value;
    }

    public function __invoke(): void
    {
        echo 'wp';
    }
}

$registry = new Registry();
$registry->parse(new HelloWorld());
```

Note

Attributes will only be applied to non-abstract public methods that are not PHP native methods or any methods that begin with `__` like `__constructor`, `__clone`, and `__callStatic`. If you add the Attributes at the class level, the class should implement the `__invoke` method, as shown in the above example.

### Removing Hook

[](#removing-hook)

You can also remove a hook similarly to how you would with the native WordPress functions:

```
$registry = new Registry();
$registry->addAction('init', 'initialise');
$registry->addFilter('the_content', 'content', 100);

// ...later in the code...
$registry->removeAction('init', 'initialise');
$registry->removeFilter('the_content', 'content', 100);
```

It is also possible to remove all hooks at once:

```
$registry = new Registry();
$registry->addAction('init', 'initialise');
$registry->addFilter('the_content', 'content', 100);

// ...later in the code...
$registry->removeAll();
```

It is possible to attach hook with method from an object instance, a static method, or a closure:

```
use Syntatis\WPHook\Registry;

$helloWorld = new HelloWorld();
$anonymous = fn () => 'Hello, World!';

$registry = new Registry();
$registry->addFilter('the_content', [$helloWorld, 'content'], 100);
$registry->addAction('init', $anonymous);
```

However, this makes it rather tricky to remove the hook later on the code since you need to pass the same object instance or the same reference to the anonymous function to the `removeAction` and `removeFilter` methods, which is not always possible.

To circumvent this, you can pass `id` to the `addAction` and `addFilter` methods, and refer the id using `@` symbol when removing the hook. For example:

```
use Syntatis\WPHook\Registry;

$helloWorld = new HelloWorld();
$anonymous = fn () => 'Hello, World!';

$registry = new Registry();
$registry->addFilter('the_content', [$helloWorld, 'content'], 100, 1, ['id' => 'the-content-hello-world']);
$registry->addAction('init', $anonymous, 10, 1, ['id' => 'init-hello-world']);

// ...much later in the code...

$registry->removeAction('init', '@init-hello-world', 10);
$registry->removeFilter('the_content', '@the-content-hello-world', 100);
```

Important

The ID must be all lowercase and use words separated by `-`, `.`, or `_`. It should not have any uppercase letters, spaces, or special characters. You can use a slash (`/`) to define the namespace, like `acme/hello-world`, to avoid conflicts with other plugins or themes. Please note that the ID added within the registry must be unique. If you're trying to add the same ID twice, it will throw an exception.

References
----------

[](#references)

- [WordPress Plugin Boilerplate](https://wppb.me/)
- [Maximum function nesting level of '100' reached, aborting!](https://wordpress.stackexchange.com/questions/147505/wp-insert-posts-fatal-error-maximum-function-nesting-level-of-100-reached-ab)
- [PHP OOP: Introduction](https://phptherightway.com/#object-oriented-programming)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 61.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~40 days

Recently: every ~17 days

Total

6

Last Release

669d ago

PHP version history (2 changes)v0.1.0PHP ^7.4 || ^8.0

v0.3.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/d56709cd680efe26767d8ecf987f66d045348965b16b86ab6dbf18e290f24ff4?d=identicon)[tfirdaus](/maintainers/tfirdaus)

---

Top Contributors

[![tfirdaus](https://avatars.githubusercontent.com/u/2067467?v=4)](https://github.com/tfirdaus "tfirdaus (26 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (14 commits)")[![rafaucau](https://avatars.githubusercontent.com/u/25438601?v=4)](https://github.com/rafaucau "rafaucau (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

object-oriented-programmingwordpresswordpress-hookswordpressfilteractionobject oriented

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/syntatis-wp-hook/health.svg)

```
[![Health](https://phpackages.com/badges/syntatis-wp-hook/health.svg)](https://phpackages.com/packages/syntatis-wp-hook)
```

###  Alternatives

[tormjens/eventy

The WordPress filter/action system in Laravel

438912.9k16](/packages/tormjens-eventy)[millat/laravel-hooks

The WordPress filter, action system in Laravel

5715.1k](/packages/millat-laravel-hooks)[mitogh/katana

Filters to control where the size of images are generated.

204.4k](/packages/mitogh-katana)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
