1. Go to this page and download the library: Download pinkcrab/wp-hook-subscriber 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/ */
pinkcrab / wp-hook-subscriber example snippets
php
class On_Single_Hook extends Abstract_Hook_Subscription {
/**
* The hook to register the subscriber
* @var string
*/
protected ?string $hook = 'some_hook';
/**
* Some service
* @param My_Service
*/
protected $my_service;
public function __construct( My_Service $my_service ) {
$this->my_service = $my_service;
}
/**
* Callback
* @param mixed ...$args
*/
public function execute( ...$args ): void {
// Args are accessed in the order they are passed.
// do_action('foo', 'first','second','third',.....);
//$args[0] = first, $args[1] = second, $args[2] = third, .....
if ( $args[0] === 'something' ) {
$this->my_service->do_something( $args[1] );
}
}
}
// Would be called by
do_action('some_hook', 'something', ['some','data','to do','something']);
php
class Deferred_Hook extends Abstract_Hook_Subscription {
/**
* The hook to register the subscriber
* @var string
*/
protected ?string $hook = 'some_hook';
/**
* Deferred hook to call
*
* @var string|null
*/
protected ?string $deferred_hook = 'some_global_populated';
/**
* Our global data
* @param Some_Global|null
*/
protected $some_global;
public function __construct() {
global $some_global;
$this->some_global = $some_global;
}
/**
* Callback
* @param mixed ...$args
*/
public function execute( ...$args ): void {
// Depends on a global which is set later than init.
if ( $args[0] === 'something' && ! empty( $this->some_global ) ) {
do_something( $this->some_global->some_property, $args[1] );
}
}
}
php
function acme_plugin_function(){
global $some_global; // Currently empty/null
$some_global = new Some_Global();
do_action('some_global_populated', ['some', 'data']);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.