shineunited/wordpress-hooks

Tool for managing WordPress hooks prior to initialization.

dev-main 2023-01-17 23:21 UTC

This package is auto-updated.

Last update: 2024-04-18 02:04:42 UTC


README

License Latest Version PHP Version Main Status Release Status Develop Status

Description

A tool for managing WordPress hooks. Allows registration of hooks prior to WordPress initialization and provides a framework for defining hooks using PHP attributes.

Installation

to add wordpress-hooks, the recommended method is via composer.

$ composer require shineunited/wordpress-hooks

HookManager

The HookManager class provides static methods for managing WordPress hooks. While most of the functions are simply aliased to the built-in WordPress functions, some of them all management of hooks prior to initialization.

use ShineUnited\WordPress\Hooks\HookManager;

function callback_function($param1, $param2) {
	// code
}

// this can be used prior to initialization
HookManager::addFilter('filter-name', 'callback-function', 10, 2);

Attribute Hooks

Hooks can also be defined by using the Hook attributes and HookManager::register();

use ShineUnited\WordPress\Hooks\Filter;
use ShineUnited\WordPress\Hooks\HookManager;

class MyObjectHooks {

	#[Filter('lowercase')]
	public function lowercase(string $value): string {
		return strtolower($value);
	}

	#[Filter('uppercase')]
	public function uppercase(string $value): string {
		return strtoupper($value);
	}
}

$hooks = new MyObjectHooks();
HookManager::register($hooks);
// WordPress Equivalent
//   add_filter('lowercase', [$hooks, 'lowercase'], 10, 1);
//   add_filter('uppercase', [$hooks, 'uppercase'], 10, 1);

Multiple hooks can be applied to a single callback.

use ShineUnited\WordPress\Hooks\Action;
use ShineUnited\WordPress\Hooks\Filter;
use ShineUnited\WordPress\Hooks\HookManager;

$closure =
#[Filter('example-filter', 12)]
#[Action('example-action')]
function($value) {
	// code
};

HookManager::register($closure);
// WordPress Equivalent:
//   add_filter('example-filter', $closure, 12, 1);
//   add_filter('example-action', $closure, 10, 1);

Function Reference

For more details and examples please see our documentation.