PHPackages                             pinkcrab/hook-loader - 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. pinkcrab/hook-loader

ActiveLibrary

pinkcrab/hook-loader
====================

An object based hook loader for WordPress.

1.2.0(3y ago)234.9k—0.2%[1 issues](https://github.com/Pink-Crab/Loader/issues)[1 PRs](https://github.com/Pink-Crab/Loader/pulls)2MITPHPPHP &gt;=7.1.0

Since Mar 9Pushed 2y agoCompare

[ Source](https://github.com/Pink-Crab/Loader)[ Packagist](https://packagist.org/packages/pinkcrab/hook-loader)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-hook-loader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (12)Versions (15)Used By (2)

Hook\_Loader
============

[](#hook_loader)

The PinkCrab Hook Hook\_Loader.

[![alt text](https://camo.githubusercontent.com/f19378e026b8d7da5e12515170fae342bd5c06aacfd7a62bba54a3169227c53a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d312e322e302d79656c6c6f772e7376673f7374796c653d666c6174 " ")](https://camo.githubusercontent.com/f19378e026b8d7da5e12515170fae342bd5c06aacfd7a62bba54a3169227c53a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d312e322e302d79656c6c6f772e7376673f7374796c653d666c6174)[![Open Source Love](https://camo.githubusercontent.com/2d4eac62d5f5830a5309100b88e0ecccb171aee1fbcd794149f4a580b0010c56/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313032)](https://github.com/ellerbrock/open-source-badge/)[![](https://github.com/Pink-Crab/Loader/workflows/GitHub_CI/badge.svg " ")](https://github.com/Pink-Crab/Loader/workflows/GitHub_CI/badge.svg)[![codecov](https://camo.githubusercontent.com/6c58f2ab18273bf20b3bb99f064f2b6eb1b0761991e5b5c7ae9d1eab96953074/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f4c6f616465722f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d39344446544156414149)](https://codecov.io/gh/Pink-Crab/Loader)

For more details please visit our docs. [https://perique.info/lib/Hook\_Loader.html](https://perique.info/lib/Hook_Loader.html)

Version
-------

[](#version)

**Release 1.2.0**

> Since v1.0.0 we have made some changes to have this all works under the hood, we have changed from Loader to Hook\_Loader as the main class, but Loader has been left in as pollyfill for older versions.

Why?
----

[](#why)

WordPress and especially WooCommerce is built around hooks and if you have to register a lot of them, it can be hard to keep track of them all and what is currently being registered with WP.

The PinkCrab Hook\_Loader gives a more manageable way of registering and removing hook call, shortcode and ajax calls.

Setup
-----

[](#setup)

```
$ composer require pinkcrab/hook-loader
```

Once you have the hook loader installed it just case of putting it to use. As this is all held as a class, you can pass the instance as depenedecnies to any class you wish to give access to the loader.

Registering Hooks (actions &amp; filters)
-----------------------------------------

[](#registering-hooks-actions--filters)

> **Hook\_Loader::action(string $hook, callable $method, int $priority=10, int $args = 1): void**

> **Hook\_Loader::admin\_action(string $hook, callable $method, int $priority=10, int $args = 1): void**

> **Hook\_Loader::front\_action(string $hook, callable $method, int $priority=10, int $args = 1): void**

> **Hook\_Loader::filter(string $hook, callable $method, int $priority=10, int $args = 1): void**

> **Hook\_Loader::admin\_filter(string $hook, callable $method, int $priority=10, int $args = 1): void**

> **Hook\_Loader::front\_filter(string $hook, callable $method, int $priority=10, int $args = 1): void**

```
$loader = new Hook_Loader();

// Add actions
$loader->action('some_action', 'my_callback'); // Registered front and admin
$loader->front_action('some_action', 'my_callback'); // Frontend only
$loader->admin_action('some_action', 'my_callback'); // Admin only

// Filters
$loader->filter('some_filter', 'strtolower'); // Registered front and admin
$loader->front_filter('some_filter', 'strtolower'); // Frontend only
$loader->admin_filter('some_filter', 'strtolower'); // Admin only

// Remove hooks
$loader->remove('some_action', 'someone_else_callback', 10);
$loader->remove_filter('some_action', 'someone_else_callback', 10); // Does the same as remove()
$loader->remove_action('some_action', 'someone_else_callback', 10); // Does the same as remove()

// Ajax and Shortcode.
$loader->shortcode('my_shortcode', 'shortcode_callback');
$loader->ajax('my_action', 'my_callback', true, true);

// Once all have been added, just process with
$loader->register_hooks();
```

### Use with a class.

[](#use-with-a-class)

```
class SomeAction{
  /**
   * Register all hooks for this class
   * @param Hook_Loader $loader
   * @return void
   */
  public function hooks(Hook_Loader $loader){
    $loader->action('action_handle', [$this, 'some_method')], 20);
  }

  // The callback
  public function some_method(){
    print 'I WAS CALLED';
  }
}

// In your code, just pass the loader to this class.
$loader = new Hook_Loader();
$some_action = new SomeAction();

// Add all the some_action hooks to loader and register them.
$some_action->hooks($loader);
$loader->register_hooks();
```

Hook Removal (actions &amp; filters)
------------------------------------

[](#hook-removal-actions--filters)

> **Hook\_Loader::remove(string $hook, callable $method, int $priority=10): void**

> **Hook\_Loader::remove\_filter(string $hook, callable $method, int $priority=10): void**

> **Hook\_Loader::remove\_hook(string $hook, callable $method, int $priority=10): void**

While remove\_action() and remove\_filter() are prefectly suitable 90% of the time, it can be tricky to unset hooks with have been added as isntance to classes, you can not recall the same instance. Out Hook\_Removal class will manually remove all hooks based on the class name (instance or static use). Allowing for the removal of hooks created by other plugins.

Even if the hook was added via a class instance, you only need to use the class name to add the method used. This allows the avoidance of having to recreate an instance of the class and potentially rerunning other hooks and setup routines.

```
// The above hook can be removed using

// Just the full class name (doesnt run autload)
$loader->remove('action_handle', [SomeAction::class, 'some_method'], 20);

// Or as a new instance
$loader->remove('action_handle', [new SomeAction(), 'some_method'], 20);
```

Shortcodes
----------

[](#shortcodes)

> **Hook\_Loader::shortcode(string $hook, callable $method): void**

You can easily add shortcodes using the loader, not only that you ensure they come with fully populated objects behind them

```
// Simple example
$loader->shortcode(
  'testShortCode',
  function( $atts ) {
    echo $atts['text'];
  }
);

// Called with shortcode as normal (either in php or wp-admin text input)
do_shortcode( "[testShortCode text='yes']" ); // yes

// Part of a class
class ShortCode {

  protected $some_service;

  public function __construct(Some_Service $some_service){
    $this->some_service = $some_service;
  }

  public function register(Hook_Loader $loader){
    $loader->shortcode('my_shortcode', [$this, ['render_shortcode']]);
  }

  public function render_shortcode(array $args){
    print $this->some_service->do_something($args['something']);
  }
}
```

Ajax
----

[](#ajax)

> **Hook\_Loader::ajax(string $hook, callable $method, bool $public, bool $private): void**

If you want to register ajax calls, it requires 2 hook calls. This soon gets messy if you are setting up multiple calls. The Hook\_Loader can handle registering either or both of this with a single declaration.

```
$loader->ajax('my_action', 'my_callback', true, true); // For both logged in and out users.
$loader->ajax('my_action', 'my_callback', false, true); // For only logged in users.
$loader->ajax('my_action', 'my_callback', true, false); // For only logged out users.
```

As with the other examples this can be used as part of class to create self contained ajax calls. While this can be done manually, the Registerables package comes with a very useful Ajax abstract class which can be used.

Filtering Hooks
---------------

[](#filtering-hooks)

It is possible access the Hook Collection before it is registered, this allows for the filtering of the hooks.

> You can either use the Hook\_Collection class constant `Hook_Collection::REGISTER_HOOKS` or its literal string value `pinkcrab/loader/register_hooks`

```
add_filter(Hook_Collection::REGISTER_HOOKS, function($hooks){
  // Do something with the hooks
  return $hooks;
});
```

License
-------

[](#license)

### MIT License

[](#mit-license)

Change Log
----------

[](#change-log)

- 1.2.0 - Updated testing dependencies and support for php8, added in the ability to filter hooks prior to registration.
- 1.1.2 - Loader::class has now been marked as deprecated
- 1.1.1 - Typo on register\_hooks() (spelt at regster\_hooks)
- 1.1.0 - All internal functionality moved over, still has the same ex
- 1.0.2 - Fixed incorrect docblock on Hook\_Loader\_Collection::pop() and adding missing readme entries for shortcode and ajax.
- 1.0.1 - Added pop() and count() to the hook collection. Not used really from outside, only in tests.
- 1.0.0 - Moved from Plugin Core package. Moved the internal collection to there own Object away from PC Collection.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

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

Recently: every ~111 days

Total

8

Last Release

1442d ago

### Community

Maintainers

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

---

Top Contributors

[![gin0115](https://avatars.githubusercontent.com/u/28779094?v=4)](https://github.com/gin0115 "gin0115 (71 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pinkcrab-hook-loader/health.svg)

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

PHPackages © 2026

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