W3cubDocs

/WordPress

Hooks::register( string $hook, callable $callback, int $priority )

Register a callback for a hook

Parameters

$hookstringrequired
Hook name
$callbackcallablerequired
Function/method to call on event
$priorityintrequired
Priority number. <0 is executed earlier, >0 is executed later

Source

public function register($hook, $callback, $priority = 0) {
	if (is_string($hook) === false) {
		throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
	}

	if (is_callable($callback) === false) {
		throw InvalidArgument::create(2, '$callback', 'callable', gettype($callback));
	}

	if (InputValidator::is_numeric_array_key($priority) === false) {
		throw InvalidArgument::create(3, '$priority', 'integer', gettype($priority));
	}

	if (!isset($this->hooks[$hook])) {
		$this->hooks[$hook] = [
			$priority => [],
		];
	} elseif (!isset($this->hooks[$hook][$priority])) {
		$this->hooks[$hook][$priority] = [];
	}

	$this->hooks[$hook][$priority][] = $callback;
}

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wporg-requests-hooks/register