Integration with Classes:
- Hook Being Class Static Method
- Hook Being Class Instance Method
- Hook Being Method of a Singleton
- Hook Added/Removed within Class
- add action with
add_action()
- invoke action with
do_action()
ordo_action_ref_array()
Hooks are not meant to return any value, they should only execute some code.
$function
as a string
add_action('prfx_plugin_pre_setup', 'pre_setup_fn', 10, 2);
function pre_setup_fn($a, $b) {
echo $a + $b;
}
$function
as a function
add_action('prfx_plugin_pre_setup', function ($a, $b) {
echo $a + $b;
}, 10, 2);
$pre_setup_fn = function ($a, $b) {
echo $a + $b;
};
add_action('prfx_plugin_pre_setup', $pre_setup_fn, 10, 2);
do_action('prfx_plugin_pre_setup', 1, 2);
This function is identical to add_action()
, but arguments are passed an an array.
do_action_ref_array('prfx_plugin_pre_setup', [1, 2]);
Priority can be omitted, but if it's passed and differs from original one passed to the
add_action()
, an action will not be removed.
- action with
$function
passed as a string
add_action('prfx_plugin_pre_setup', 'pre_setup_fn', 10, 2);
...
remove_action('prfx_plugin_pre_setup', 'pre_setup_fn');
- action with
$function
passed as a function
add_action('prfx_plugin_pre_setup', $pre_setup_fn, 10, 2);
...
remove_action('prfx_plugin_pre_setup', $pre_setup_fn);
Note that
remove_action()
must be called inside a function and cannot be called directly:
add_action('wp_head', function () {
remove_action('wp_footer', 'no_longer_needed_action');
});
class Hook {
public static function calc_hook($a, $b) {
echo $a + $b;
}
}
add_action('prfx_plugin_pre_setup', ['Hook', 'calc_hook'], 10, 2);
...
remove_action('prfx_plugin_pre_setup', ['Hook', 'calc_hook']);
class Hook {
public function calc_hook($a, $b) {
echo $a + $b;
}
}
$hook = new Hook();
add_action('prfx_plugin_pre_setup', [$hook, 'calc_hook'], 10, 2);
...
remove_action('prfx_plugin_pre_setup', [$hook, 'calc_hook']);
class Hook {
private static $_instance = null;
private function __construct() {}
public static function getInstance() {
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function calc_hook($a, $b) {
echo $a + $b;
}
}
add_action('prfx_plugin_pre_setup', [Hook::getInstance(), 'calc_hook'], 10, 2);
...
remove_action('prfx_plugin_pre_setup', [Hook::getInstance(), 'calc_hook']);
class Hook {
public function addHooks() {
add_action('prfx_plugin_pre_setup', [$this, 'calc_hook'], 10, 2);
}
public function removeHooks() {
remove_action('prfx_plugin_pre_setup', [$this, 'calc_hook']);
}
public function calc_hook($a, $b) {
echo $a + $b;
}
}
$hook = new Hook();
$hook->addHooks();
...
$hook->removeHooks();
class Hook {
public static function addHooks() {
add_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook'], 10, 2);
}
public static function removeHooks() {
remove_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook']);
}
public static function calc_hook($a, $b) {
echo $a + $b;
}
}
Hook::addHooks();
...
Hook::removeHooks();
class Hook {
public function addHooks() {
add_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook'], 10, 2);
}
public function removeHooks() {
remove_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook']);
}
public static function calc_hook($a, $b) {
echo $a + $b;
}
}
$hook = new Hook();
$hook->addHooks();
...
$hook->removeHooks();
class Hook {
public static function addHooks() {
add_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook'], 10, 2);
}
public static function removeHooks() {
remove_action('prfx_plugin_pre_setup', [get_called_class(), 'calc_hook']);
}
public function calc_hook($a, $b) {
echo $a + $b;
}
}
$hook = new Hook();
$hook::addHooks();
# or: Hook::addHooks()
...
$hook::removeHooks();
# or: Hook::removeHooks()