/wp-hook

🪝 WordPress hook with OOP flavor

Primary LanguagePHPGNU General Public License v2.0GPL-2.0

🪝 wp-hook

WordPress hook with object-oriented programming

Packagist Dependency Version wp codecov


A lightweight class wrapper designed to enable the use of WordPress hooks in an object-oriented manner. Inspired by the WordPress Plugin Boilerplate, this package introduces a cleaner syntax and enhanced features for managing hooks in your WordPress plugin or theme.

Motivation

Managing a complex WordPress site often involves dealing with numerous hooks, which can quickly become unmanageable and prone to errors. A common issue is the nesting of hooks, which can lead to scenarios where certain hooks are never executed due to their initialization timing:

add_action('init', 'initialise');

function initialise(): void
{
    add_action('after_setup_theme', 'hello_world');
}

function hello_world(): void
{
    echo 'Hello World';
}

In the above example, hello_world may never be executed if after_setup_theme has already been fired before the init hook. In extreme cases, such nested hooks can cause errors like maximum function nesting level reached. This package aims to minimize these pitfalls by providing a more structured approach to managing hooks.

Installation

Install the package via Composer:

composer require syntatis/wp-hook

Usage

Create a new instance of the Hook class and register your hooks:

use Syntatis\WPHook\Hook;

$hook = new Hook();
$hook->addAction('init', 'initialise');
$hook->addFilter('the_content', 'content', 100);
$hook->addAction('add_option', 'option', 100, 2);
$hook->register();

Using PHP Attributes

If your theme or plugin runs on PHP 8.0 or later, you can leverage PHP Attributes to define hooks directly within your classes:

use Syntatis\WPHook\Action;
use Syntatis\WPHook\Filter;
use Syntatis\WPHook\Hook;

#[Action(name: "wp")]
class HelloWorld
{
    #[Action(name: "init")]
    public function initialise(): void
    {
        echo 'initialise';
    }

    #[Filter(name: "the_content", priority: 100)]
    public function content(string $content): string
    {
        return $content . "\ncontent";
    }

    #[Action(name: "the_content", priority: 100, acceptedArgs: 2)]
    public function option(string $optionName, mixed $value): void
    {
        echo $optionName . $value;
    }

    public function __invoke(): void
    {
        echo 'wp';
    }
}

$hook = new Hook();
$hook->parse(new HelloWorld());
$hook->register();

Note

Attributes will only be applied to non-abstract public methods that are not PHP native methods or any methods that begin with __ like __constructor, __clone, and __callStatic. If you add the Attributes at the class level, the class should implement the __invoke method, as shown in the above example.

Deregistering Hooks

You can also deregister hooks, which will remove all the actions and filters that have been registered in the Hook instance:

$hook = new Hook();
$hook->addAction('init', 'initialise');
$hook->addFilter('the_content', 'content', 100);
$hook->parse(new HelloWorld());
$hook->register();

// ...later in the code...
$hook->deregister();

References