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

ActiveLibrary[Search &amp; Filtering](/categories/search)

piotrpress/wordpress-hooks
==========================

This library uses PHP Attributes (introduced in PHP version 8.0) to automagically add/remove WordPress Hooks (Filters and Actions) to/from functions and methods.

v5.3.0(1y ago)3722013GPL-3.0PHPPHP &gt;=7.4

Since Dec 14Pushed 1y ago4 watchersCompare

[ Source](https://github.com/PiotrPress/wordpress-hooks)[ Packagist](https://packagist.org/packages/piotrpress/wordpress-hooks)[ Docs](https://github.com/PiotrPress/wordpress-hooks)[ RSS](/packages/piotrpress-wordpress-hooks/feed)WikiDiscussions 5.x Synced 1mo ago

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

WordPress Hooks
===============

[](#wordpress-hooks)

This library uses [PHP Attributes](https://www.php.net/manual/en/language.attributes.overview.php) (introduced in PHP version `8.0`) to automagically add/remove [WordPress Hooks](https://developer.wordpress.org/plugins/hooks/) ([Filters](https://codex.wordpress.org/Plugin_API/Filter_Reference) and [Actions](https://codex.wordpress.org/Plugin_API/Action_Reference)) to/from functions and methods.

**Note:** The library supports PHP &gt;= `7.4` version.

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

[](#installation)

```
$ composer require piotrpress/wordpress-hooks
```

### Load

[](#load)

```
require __DIR__ . '/vendor/autoload.php';
```

Usage
-----

[](#usage)

### Attributes

[](#attributes)

```
#[ Action( string $name, int $priority = 10 ) ]
#[ Filter( string $name, int $priority = 10 ) ]
```

### Functions

[](#functions)

```
Hooks::add( object $object = null, string $callback = '', PiotrPress\CacherInterface $cache = null ) : void
Hooks::remove( object $object = null, string $callback = '', PiotrPress\CacherInterface $cache = null ) : void
```

Examples
--------

[](#examples)

### Hooks::add/remove( $object )

[](#hooksaddremove-object-)

If `object` argument is passed and `callback` is omitted, then all hooks from object are added or removed.

```
use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    public function add_hooks() {
        Hooks::add( $this );
    }

    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$example->add_hooks();

Hooks::remove( $example );
```

This is an equivalent to:

```
$example = new Example();

add_action( 'init', [ $example, 'example_init' ] );
add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

remove_action( 'init', [ $example, 'example_init' ] );
remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );
```

**Note:** `Hooks::add/remove()` methods can be called from the method, or even outside the object.

### Hooks::add/remove( $object, $callback )

[](#hooksaddremove-object-callback-)

If `object` and `callback` arguments are passed, then only hooks for this method are added or removed.

```
use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    public function add_hooks() {
        Hooks::add( $this, 'example_init' );
        Hooks::add( $this, 'example_the_title' );
    }

    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$example->add_hooks();

Hooks::remove( $example, 'example_init' );
Hooks::remove( $example, 'example_the_title' );
```

This is an equivalent to:

```
$example = new Example();

add_action( 'init', [ $example, 'example_init' ] );
add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

remove_action( 'init', [ $example, 'example_init' ] );
remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );
```

### Hooks::add/remove( callback: $callback )

[](#hooksaddremove-callback-callback-)

If `object` argument is omitted and `callback` is passed, then only hooks for this function are added or removed.

```
use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

#[ Action( 'init' ) ]
public function example_init() : void {
    // do something
}

#[ Filter( 'the_title', 1 ) ]
public function example_the_title( string $post_title, int $post_id ) : string {
    // do something
}

Hooks::add( callback: 'example_init' );
Hooks::add( callback: 'example_the_title' );

Hooks::remove( callback: 'example_init' );
Hooks::remove( callback: 'example_the_title' );
```

This is an equivalent to:

```
add_action( 'init', 'example_init' );
add_filter( 'the_title', 'example_the_title', 1, 2 );

remove_action( 'init', 'example_init' );
remove_filter( 'the_title', 'example_the_title', 1, 2 );
```

Cache
-----

[](#cache)

Optionally, you can pass a cache object, which must implement [PiotrPress\\CacherInterface](https://github.com/PiotrPress/cacher/blob/master/src/CacherInterface.php) interface, as a third `cache` argument to `Hooks::add/remove()` methods.

This will cache the result of `Hooks::get()` method, which provides a list of hooks for a given object, method or function using [Reflection API](https://www.php.net/manual/en/book.reflection.php), so caching its result can significantly improve the performance.

### Example

[](#example)

```
use PiotrPress\Cacher;
use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$cache = new Cacher( '.hooks' );

Hooks::add( object: $example, cache: $cache );
Hooks::remove( object: $example, cache: $cache );
```

**Note:** You can use simple file-based cache, which is provided by [PiotrPress\\Cacher](https://github.com/PiotrPress/cacher) library distributed with this library.

Kudos
-----

[](#kudos)

Inspirations, feedback, ideas and feature requests provided by:

- [Jakub Mikita](https://github.com/jakubmikita)
- [Sebastian Pisula](https://github.com/sebastianpisula)
- [Mateusz Gbiorczyk](https://github.com/gbiorczyk)
- [Krzysztof Grabania](https://github.com/Dartui)
- [Dominik Kawula](https://github.com/domkawula)
- [Jacek Sławiński](https://github.com/jacekslawinski)

Troubleshooting
---------------

[](#troubleshooting)

**Note:** Named arguments not working in PHP &lt; `8.0` version.

Requirements
------------

[](#requirements)

PHP &gt;= `7.4` version.

License
-------

[](#license)

[GPL3.0](license.txt)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~114 days

Recently: every ~88 days

Total

13

Last Release

609d ago

Major Versions

v1.0.0 → v2.0.02021-03-23

2.x-dev → v3.0.02021-03-23

3.x-dev → v4.0.02021-11-07

4.x-dev → v5.0.02023-09-29

PHP version history (3 changes)1.x-devPHP &gt;=8.0

v2.0.0PHP ^7.4

v4.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10326736?v=4)[Piotr Niewiadomski](/maintainers/PiotrPress)[@PiotrPress](https://github.com/PiotrPress)

---

Top Contributors

[![PiotrPress](https://avatars.githubusercontent.com/u/10326736?v=4)](https://github.com/PiotrPress "PiotrPress (5 commits)")

---

Tags

actionsattributesfiltershookhooksphp-attibutesphp8wordpresswordpress-composerwordpress-hookswordpress-librarywordpress-oopwordpress-oop-phpwordpress-packagewordpress-pluginwpwp-pluginwordpressfilterattributeshookswpHOOKfiltersactionsactionphp8attributephp-attributesphp 8.0php attribute

### Embed Badge

![Health badge](/badges/piotrpress-wordpress-hooks/health.svg)

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

###  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)[x-wp/di

The dependency injection container for WordPress

301.1k10](/packages/x-wp-di)

PHPackages © 2026

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