PHPackages                             morningtrain/wp-hooks - 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. morningtrain/wp-hooks

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

morningtrain/wp-hooks
=====================

A system for organizing WordPress hooks

v0.3.2(3y ago)22.2k11MITPHPPHP ^8.0

Since Jun 14Pushed 3y ago4 watchersCompare

[ Source](https://github.com/Morning-Train/wp-hooks)[ Packagist](https://packagist.org/packages/morningtrain/wp-hooks)[ RSS](/packages/morningtrain-wp-hooks/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (8)Used By (1)

WP Hooks
========

[](#wp-hooks)

To let you organize all your WordPress actions and filters.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Getting Started](#getting-started)
    - [Installation](#installation)
- [Dependencies](#dependencies)
    - [morningtrain/php-loader](#morningtrainphp-loader)
- [Usage](#usage)
    - [Creating a hook](#creating-a-hook)
        - [Adding an action](#adding-an-action)
        - [Adding a filter](#adding-a-filter)
        - [Adding a view on an action](#adding-a-view-on-an-action)
- [Credits](#credits)
- [Testing](#testing)
- [License](#license)

Introduction
------------

[](#introduction)

This tool is made for organizing WordPress hooks.

This tool lets you:

- Load all .php files recursively in a directory
- Add filters and action using a fluid api
- Render Blade views directly on an action (if morningtrain/wp-view is installed)

Getting Started
---------------

[](#getting-started)

To get started install the package as described below in [Installation](#installation).

To use the tool have a look at [Usage](#usage)

### Installation

[](#installation)

Install with composer

```
composer require morningtrain/wp-hooks
```

Dependencies
------------

[](#dependencies)

### morningtrain/php-loader

[](#morningtrainphp-loader)

[PHP Loader](https://github.com/Morning-Train/php-loader) is used to load and initialize all Hooks

### morningtrain/wp-view (optional)

[](#morningtrainwp-view-optional)

[WP View](https://github.com/Morning-Train/wp-view) is used to load and initialize all Hooks

Usage
-----

[](#usage)

To load all Hooks of a given directory

```
// Load all .php files in ./Hooks and add all found Hooks
\Morningtrain\WP\Hooks\Hook::loadDir(__DIR__ . "/App/Hooks");
```

### Multiple Directories

[](#multiple-directories)

Since this tool uses PHP Loader, you may use multiple directories.

```
// Load all .php files in ./Hooks and add all found Hooks
\Morningtrain\WP\Hooks\Hook::loadDir([__DIR__ . "/App/Hooks",__DIR__ . "/EvenMoreHooks"]);
```

Creating a Hook
---------------

[](#creating-a-hook)

To create a hook first call `Hook::action`, `Hook::filter` or `Hook::view`. Then start a chain to add additional parameters.

### Adding an action

[](#adding-an-action)

To add an action call `Hook::action`. You may either add the callback as the second parameter or by using `handle()`

```
\Morningtrain\WP\Hooks\Hook::action('init',[Some::class, 'someMethod']);
// is the same as
\Morningtrain\WP\Hooks\Hook::action('init')
    ->handle([Some::class, 'someMethod']);

// With a priority
\Morningtrain\WP\Hooks\Hook::action('init')
    ->priority(20)
    ->handle([Some::class, 'someMethod']);

// Rendering a view
\Morningtrain\WP\Hooks\Hook::action('init')
    ->view('some_view');
```

**Note** that it is not necessary to define the number of args for the callback. The action (or filter) will look at the callback's definition to know how many arguments it takes.

### Adding a filter

[](#adding-a-filter)

Adding filters is just like adding action. Call `Hook::filter`. You may either add the callback as the second parameter or by using `filter()`

```
\Morningtrain\WP\Hooks\Hook::filter('mime_types',[Some::class, 'addSvgMimeType']);
// is the same as
\Morningtrain\WP\Hooks\Hook::filter('mime_types')
    ->filter([Some::class, 'addSvgMimeType']);

// With a priority
\Morningtrain\WP\Hooks\Hook::filter('mime_types')
    ->priority(20)
    ->filter([Some::class, 'addSvgMimeType']);

// For simple filters that return true or false
\Morningtrain\WP\Hooks\Hook::filter('use_some_feature')
    ->returnTrue();
\Morningtrain\WP\Hooks\Hook::filter('use_some_feature')
    ->returnFalse();
```

**Note** that it is not necessary to define the number of args for the callback. The action (or filter) will look at the callback's definition to know how many arguments it takes.

### Adding a view on an action

[](#adding-a-view-on-an-action)

You may, if the `morningtrain/wp-view` package is installed, render a blade view directly from an action.

```
    // This will render the footer/copyright view in the footer
    \Morningtrain\WP\Hooks\Hook::view('footer','footer/copyright');
```

**Note** that you MUST define the number of args used in the hook since the hook has no callback method to analyze.

If you need to use the action params in your view you may render your view from another method or use [view composing](https://laravel.com/docs/9.x/views#view-composers).

```
// An action that looks something like this: do_action('some_post_action',$postId);
\Morningtrain\WP\Hooks\Hook::view('some_post_action','someView');
\Morningtrain\WP\View\View::composer('some_post_action',function($view){
    [$postId] = $view;
    $view->with('postId',$postId);
});
```

### Using single use handlers (invokable)

[](#using-single-use-handlers-invokable)

You can use a single use class like so:

```
class FilterMimeTypes{
 public function __invoke(array $mimeTypes)
        {
            $mimeTypes['webp'] = 'image/webp';
            return $mimeTypes;
        }
}

\Morningtrain\WP\Hooks\Hook::filter('mime_types',FilterMimeTypes::class);
```

Credits
-------

[](#credits)

- [Mathias Munk](https://github.com/mrmoeg)
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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

Every ~18 days

Recently: every ~26 days

Total

7

Last Release

1364d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/278725?v=4)[morningtrain](/maintainers/morningtrain)[@morningtrain](https://github.com/morningtrain)

![](https://avatars.githubusercontent.com/u/5990117?v=4)[Mathias Munk](/maintainers/mrmoeg)[@mrmoeg](https://github.com/mrmoeg)

---

Top Contributors

[![mrmoeg](https://avatars.githubusercontent.com/u/5990117?v=4)](https://github.com/mrmoeg "mrmoeg (32 commits)")

---

Tags

wordpresshooksmorningtrain

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/morningtrain-wp-hooks/health.svg)

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

###  Alternatives

[tormjens/eventy

The WordPress filter/action system in Laravel

438937.3k22](/packages/tormjens-eventy)[x-wp/di

The dependency injection container for WordPress

314.2k13](/packages/x-wp-di)

PHPackages © 2026

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