1. Go to this page and download the library: Download pollora/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/ */
pollora / hook example snippets
use Pollora\Hook\Action;
use Pollora\Hook\Filter;
Action::add('init', function (): void {
// runs on WordPress init
});
Filter::add('the_content', fn (string $content): string => $content.'<p>Appended!</p>');
Action::do('my_custom_action', $order, $user);
$title = Filter::apply('my_title_filter', $title, $post);
if (Action::exists('init')) {
Action::remove('init', $callback);
}
Action::add('wp_loaded', MyInitializer::class);
// Resolves to [new MyInitializer, 'wpLoaded']
Action::add('save_post', [CrmSync::class, 'push']);
// An instance method named by class resolves to [new CrmSync, 'push']
use Pollora\Hook\Adapter\Out\WordPress\Action;
use Pollora\Hook\Domain\Contract\CallbackResolverInterface;
use Psr\Container\ContainerInterface;
final class ContainerResolver implements CallbackResolverInterface
{
public function __construct(private ContainerInterface $container) {}
public function resolve(string $className): object
{
return $this->container->get($className);
}
}
$action = new Action;
$action->setCallbackResolver(new ContainerResolver($container));
$action->add('wp_loaded', MyInitializer::class);
// MyInitializer is built by the container, then wpLoaded() is hooked
use Pollora\Hook\Action;
Action::add('save_post_event', [CrmSync::class, 'push'])->async();
use Pollora\Hook\Async\AsyncContext;
final class CrmSync
{
public function push(int $postId, WP_Post $post, AsyncContext $context): void
{
$context->userId; // user who fired the hook
$context->dispatchedAt; // trigger date
$context->attempt; // attempt number
$context->get('status'); // value recorded by capture()
}
}