PHPackages                             ssnepenthe/wp-event-dispatcher - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ssnepenthe/wp-event-dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ssnepenthe/wp-event-dispatcher
==============================

An event dispatcher abstraction over WordPress hooks

0.1.0(2y ago)1452MITPHPPHP ^7.4 || ^8.0

Since Nov 19Pushed 2y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (2)Used By (0)

wp-event-dispatcher
===================

[](#wp-event-dispatcher)

A simple event dispatcher abstraction over WordPress hooks.

Warning
-------

[](#warning)

This package is currently in development and is subject to breaking changes without notice until v1.0 has been tagged.

It is one in a series of [WordPress toys](https://github.com/ssnepenthe?tab=repositories&q=topic%3Atoy+topic%3Awordpress&type=&language=&sort=) I have been working on with the intention of exploring ways to modernize the feel of working with WordPress.

As the label suggests, it should be treated as a toy.

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

[](#installation)

```
composer require ssnepenthe/wp-event-dispatcher
```

Overview
--------

[](#overview)

Event dispatchers (`WpEventDispatcher\EventDispatcherInterface`) encourage the encapsulation of events into dedicated event classes and event listeners into dedicated subscriber classes.

Usage
-----

[](#usage)

The event dispatcher is intended to be used in place of direct calls to the various action and filter functions provided by WordPress.

Event dispatchers do away with the idea of actions and filters altogether. Events are wrapped up in standalone event classes. If you want to "filter" a value, do so using typed properties or setters and getters on the event class. Event listeners can be standalone callables, but should preferably be implemented via subscriber classes.

This is roughly modeled after the Symfony event dispatcher component, but uses WordPress hooks behind the scenes.

The primary methods you will use are `dispatch`, `addListener`, and `addSubscriber`.

By default `dispatch` uses the FQCN of the event class as the event name. If an event needs a dynamic name it should implement the `WpEventDispatcher\NamedEventInterface` interface.

Event dispatchers might require a bit extra boilerplate code as compared to standard WordPress hooks, but provide a degree of type-safety and autocompletion you wouldn't otherwise get.

Below is a simple example of how you might replace a WordPress action:

Before:

```
add_action('my_plugin_initialized', function () {
    // ...
});

do_action('my_plugin_initialized');
```

After:

```
class MyPluginInitialized
{
}

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener(MyPluginInitialized::class, function (MyPluginInitialized $event) {
    // ...
});

$eventDispatcher->dispatch(new MyPluginInitialized());
```

And an example of how you might replace a filter:

Before:

```
add_filter('my_plugin_filtered_value', function ($value) {
    if (is_string($value)) {
        $value = modifyValue($value);
    }

    return $value;
});

$default = 'some string';
$value = apply_filters('my_plugin_filtered_value', $default);

if (! is_string($value)) {
    $value = $default;
}
```

After:

```
class MyPluginFilteredValue
{
    public function __construct(public string $value)
    {
    }
}

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener(MyPluginFilteredValue::class, function (MyPluginFilteredValue $event) {
    $event->value = modifyValue($event->value);
});

$event = new MyPluginFilteredValue('some string');
$eventDispatcher->dispatch($event);
$value = $event->value;
```

Event dispatchers also allow for event listeners to be grouped logically within a subscriber class.

Subscribers should implement the `WpEventDispatcher\SubscriberInterface` interface.

This interface has a single method - `getSubscribedEvents`. It should return an array in any of the following formats:

Array keys are hook tag names, values are method names on this subscriber instance to use as listeners.

```
return [
    'the_content' => 'onTheContent',
];
```

Array keys are hook tag names, values are arrays with method names at index 0 and optional priority at index 1.

```
return [
    'the_content' => ['onTheContent', 20],
];
```

Array keys are hook tag names, values are arrays of array with method names at index 0 and optional priority at index 1.

```
return [
    'the_content' => [
        ['onTheContent', 20],
        ['alsoOnTheContent'],
    ],
];
```

For (a very contrived) example:

```
class PostContentSubscriber implements SubscriberInterface
{
    public function getSubscribedEvents(): array
    {
        return [
            'the_content' => [
                ['appendSomething'],
                ['prependAnotherThing', 20],
            ],
        ];
    }

    public function appendSomething($content)
    {
        return $content . ' something';
    }

    public function prependAnotherThing($content)
    {
        return 'another thing ' . $content;
    }
}
```

Initialize the subscriber with the `addSubscriber` method on the event dispatcher:

```
$eventDispatcher->addSubscriber(new PostContentSubscriber());
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

911d ago

### Community

Maintainers

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

---

Top Contributors

[![ssnepenthe](https://avatars.githubusercontent.com/u/10903810?v=4)](https://github.com/ssnepenthe "ssnepenthe (9 commits)")

---

Tags

toywordpress

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ssnepenthe-wp-event-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/ssnepenthe-wp-event-dispatcher/health.svg)](https://phpackages.com/packages/ssnepenthe-wp-event-dispatcher)
```

PHPackages © 2026

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