PHPackages                             alleyinteractive/wp-captain-hook - 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. alleyinteractive/wp-captain-hook

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

alleyinteractive/wp-captain-hook
================================

Tools for manipulating private action/filter callbacks in WordPress.

v1.0.0(1y ago)12211.7k↑14.5%[1 PRs](https://github.com/alleyinteractive/wp-captain-hook/pulls)GPL-2.0-or-laterPHPPHP ^8.0

Since Sep 9Pushed 5mo ago20 watchersCompare

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

READMEChangelog (1)Dependencies (3)Versions (4)Used By (0)

Captain Hook
============

[](#captain-hook)

Captain Hook is a library for WordPress developers to help them manipulate objects and methods hooked into actions and filters using otherwise-inaccessible object instances.

About
-----

[](#about)

Oftentimes, a plugin will create an instance of an object in a function scope, or otherwise simply outside of the global scope, and then use that instance to hook methods into actions or filters. Here's an example:

```
class Plugin_Main {
  public function __construct(
    protected \Logger $logger
  ) {
    add_action( 'init', [ $this, 'init' ], 1 );
    add_filter( 'the_content', [ $this, 'filter_the_content' ], 99 );
  }

  public function init() {
    register_post_type( 'book' );
    register_taxonomy( 'genre', 'book' );
  }

  public function filter_the_content( $content ) {
    if ( preg_match_all( '/\[([^\)]+)\]\((\d+)\)/', $content, $matches, PREG_SET_ORDER ) ) {
      foreach ( $matches as $match ) {
        $book = get_post( $match[2] );
        if ( $book ) {
          $content = str_replace( $match[0], '' . $match[1] . '', $content );
        } else {
          $this->logger->error( 'Book not found: ' . $match[2] );
        }
      }
    }
    return $content;
  }

  public function get_logger() {
    return $this->logger;
  }

  public function set_logger( \Logger $logger ) {
    $this->logger = $logger;
  }
}
add_action( 'after_setup_theme', function () {
  new Plugin_Main( new \Logger( 'path/to/file.log' ) );
} );
```

If a developer wanted to unhook or otherwise alter the `init` method from the `init` action, they would need to have access to the `$plugin` instance. This is not possible from outside the scope of the anonymous function that created the instance.

Captain Hook provides a way for developers to access and manipulate these objects and methods, even when they are not accessible from the global scope. Features of this library include:

- **Remove Action or Filter by Force**: Remove an action or filter from a hook.
- **Reprioritize Actions and Filters**: Change the priority of an action or filter method.
- **Retrieve an object instance**: Get the otherwise-inaccessible object instance for a method hooked into an action or filter.

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

[](#installation)

Install the latest version with:

```
$ composer require alleyinteractive/wp-captain-hook
```

Basic usage
-----------

[](#basic-usage)

### Remove Action or Filter by Force

[](#remove-action-or-filter-by-force)

```
