PHP code example of alleyinteractive / wp-captain-hook

1. Go to this page and download the library: Download alleyinteractive/wp-captain-hook library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alleyinteractive / wp-captain-hook example snippets


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], '<a href="' . get_permalink( $book ) . '">' . $match[1] . '</a>', $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' ) );
} );


use function Alley\WP\remove_action_by_force;
use function Alley\WP\remove_filter_by_force;

remove_action_by_force( 'init', [ '\Plugin_Main', 'init' ], 1 );
remove_filter_by_force( 'the_content', [ '\Plugin_Main', 'filter_the_content' ], 99 );


use function Alley\WP\reprioritize_action;
use function Alley\WP\reprioritize_filter;

reprioritize_action( 'init', [ '\Plugin_Main', 'init' ], 1, 10 );
reprioritize_filter( 'the_content', [ '\Plugin_Main', 'the_content' ], 99, 20 );


use function Alley\WP\get_hooked_object;

$plugin = get_hooked_object( 'init', [ '\Plugin_Main', 'init' ], 1 );
$plugin->set_logger( new \Redis_Logger() );