This page is pulled from project's readme. Links could be broken.

Latest Stable Version Total Downloads Build Status PHP from Packagist codecov StyleCI License Donate via PayPal Hire Typist Tech

The Goals, or What This Package Does?

Using PSR-11 container implementation in WordPress plugins, themes and packages during WordPress action/filter callbacks.

Dependencies are usually lazy loaded(depends on your container implementation), not instantiated until the first time they are used (during WordPress action/filter callbacks).


Installation should be done via composer, details of how to install composer can be found at

You need a psr/container-implementation package as well. This readme uses league/container as an example (any psr/container-implementation works similarly).

# league/container is an example, any psr/container-implementation package works
$ composer require typisttech/wp-contained-hook league/container


use League\Container\Container;
use TypistTech\WPContainedHook\Hooks\Action;
use TypistTech\WPContainedHook\Hooks\Filter;
use TypistTech\WPContainedHook\Loader;

$container = new Container;

// Configure the container.
// This depends on your `psr/container-implementation`.
$container->add('bar', Bar::class);
$container->add('foo', Foo::class);

// Action.
$action = new Action('bar', 'admin_init', 'doSomething');

// Filter.
$filter = new Filter('foo', 'the_content', 'filterSomething');

// Add to loader.
$loader = new Loader($container);
$loader->add($action, $filter);

// Add to WordPress.

In plain WordPress, the above is similar to:

$bar = new Bar();
add_action('admin_init', [$bar, 'doSomething'])

$foo = new Foo();
add_filter('the_content', [$foo, 'filterSomething'])

In WordPress plus container, the above is similar to:

add_action('admin_init', function ($arg) use ($container): void {
  $bar = $container->get('bar');

add_filter('the_content', function ($arg) use ($container) {
  $foo = $container->get('foo');
  return $foo->filterSomething($arg);



Register all actions and filters for the plugin/package/theme.

Maintain a list of all hooks that are registered throughout the plugin, and register them with the WordPress API. Call the run function to execute the list of actions and filters.

Loader Constructor

  • @param Psr\Container\ContainerInterface $container The container.


$container = new Container;
$loader = new Loader($container);

Loader::add(HookInterface ...$hooks)

Add new hooks to the collection to be registered with WordPress.

  • @param HookInterface|HookInterface[] ...$hooks Hooks to be registered.


// Action.
$action = new Action(SomeClass::class, 'plugin_loaded', 'doSomething');

// Filter.
$filter = new Filter(SomeClass::class, 'the_content', 'filterSomething');

// Add to loader
$loader->add($action, $filter);


Register the hooks to the container and WordPress.



Hooks: Action and Filter

Holds necessary information for an action or a filter.

Both Action and Filter are subclasses of AbstractHook and implements HookInterface.

AbstractHook Constructor.

  • @param string $hook The name of the WordPress hook that is being registered.
  • @param string $classIdentifier Identifier of the entry to look for from container.
  • @param string $callbackMethod The callback method name.
  • @param int|null $priority Optional.The priority at which the function should be fired. Default is 10.
  • @param int|null $acceptedArgs Optional. The number of arguments that should be passed to the $callback. Default is 1.


$action = new Action('bar', 'admin_init', 'doSomething', 20, 2);

$filter = new Filter('foo', 'the_content', 'filterSomething', 20, 2);

Frequently Asked Questions

Do you have an example plugin that use this package?

Here you go:

Add your own plugin here

It looks awesome. Where can I find some more goodies like this?

This plugin isn't on Where can I give a :star::star::star::star::star: review?


Consider writing a blog post, submitting pull requests, donating or hiring me instead.

This plugin isn't on Where can I make a complaint?

To be honest, I don't care.

If you really want to share your 1-star review, send me an email - in the first paragraph, state why didn't invest your time reading the source code and making pull requests.


Donate via PayPal Donate via PayPal

Love WP Contained Hook? Help me maintain WP Contained Hook, a donation here can help with it.

Why don't you hire me?

Ready to take freelance WordPress jobs. Contact me via the contact form here or, via email [email protected]

Want to help in other way? Want to be a sponsor?

Contact: Tang Rufus

Running the Tests

Run the tests:

$ composer test
$ composer check-style


Please provide feedback! We want to make this library useful in as many projects as possible. Please submit an issue and point out what you do and don't like, or fork the project and make suggestions. No issue is too small.

Change log

Please see CHANGELOG for more information on what has changed recently.


If you discover any security related issues, please email [email protected] instead of using the issue tracker.


WP Contained Hook is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.

Full list of contributors can be found here.


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