$hooks->addHook() method

Hook a function/method to a hookable method call in this object

Hookable method calls are methods preceded by three underscores. You may also specify a method that doesn't exist already in the class The hook method that you define may be part of a class or a globally scoped function.

If you are hooking a procedural function, you may omit the $toObject and instead just call via: $this->addHook($method, 'function_name'); or $this->addHook($method, 'function_name', $options);

Usage

// basic usage
$string = $hooks->addHook(Wire $object, $method, $toObject);

// usage with all arguments
$string = $hooks->addHook(Wire $object, $method, $toObject, $toMethod = null, array $options = []);

Arguments

NameType(s)Description
objectWire
methodstring, array

Method name to hook into, NOT including the three preceding underscores. May also be Class::Method for same result as using the fromClass option. May also be array OR CSV string of either of the above to add multiple (since 3.0.137).

toObjectobject, null, callable

Object to call $toMethod from, Or null if $toMethod is a function outside of an object, Or function|callable if $toObject is not applicable or function is provided as a closure.

toMethod (optional)string, array

Method from $toObject, or function name to call on a hook event, or $options array.

options (optional)array

See $defaultHookOptions at the beginning of this class. Optional.

Return value

string

A special Hook ID that should be retained if you need to remove the hook later. If the $method argument was a CSV string or array of multiple methods to hook, then CSV string of hook IDs will be returned, and the same CSV string can be used with removeHook() calls. (since 3.0.137).

Exceptions

Method can throw exceptions on error:

  • WireException


$hooks methods and properties

API reference based on ProcessWire core version 3.0.248

Latest news

  • ProcessWire Weekly #582
    The 582nd issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 5 July 2025
  • New ProcessWire admin redesign
    When you upgrade to ProcessWire 3.0.248 or newer (currently the dev branch), you’ll immediately notice something new and beautiful…
    Blog / 9 May 2025
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits