PHP code example of hokoo / wp-hooks-dispatcher

1. Go to this page and download the library: Download hokoo/wp-hooks-dispatcher 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/ */

    

hokoo / wp-hooks-dispatcher example snippets


switch_to_blog(1);

$siteOneCleanup = create_cleanup_service_for_the_current_site();
add_action('deleted_post', [$siteOneCleanup, 'handle']);

switch_to_blog(2);

// The callback belonging to site 1 is still registered and will be called.
do_action('deleted_post', 42);

use iTRON\wpHooksDispatcher\ActionDispatcher;

$dispatcher = new ActionDispatcher();

$subscription = $dispatcher->subscribe(
    'deleted_post',
    static function (int $postId): void {
        // Work that belongs only to the site active at subscription time.
    },
    priority: 10,
    acceptedArguments: 1
);

$subscription->unsubscribe();
$subscription->unsubscribe(); // Safe: unsubscription is idempotent.

use iTRON\wpHooksDispatcher\FilterDispatcher;

$dispatcher = new FilterDispatcher();

$subscription = $dispatcher->subscribe(
    'the_title',
    static fn (string $title, int $postId): string =>
        $title . ' #' . $postId,
    priority: 10,
    acceptedArguments: 2
);

$subscription->unsubscribe();