I am trying to log each option update (later i will filter out the unnecessary ones). I have came across the 'update_option' and 'updated_option' hooks. They are triggering but the problem is that $old_value and $value are empty and/or not defined.
When I do not use default values for the $old_value and $value I get error that too few parameters was passed into the function. But according to WP documentation I should get both of them.
Any idea what´s wrong?
public function __construct()
{
add_action('update_option', [$this, 'update']);
}
/**
* @param $option string Name of the option to update
* @param $old_value mixed Old option value
* @param $value mixed New option value
*/
public function update($option, $old_value = "", $value = "")
{
/** @var $current_user WP_User */
global $current_user;
if ($old_value || $value) {
var_dump('old: ' . $old_value);
var_dump('new: ' . $value);
die;
}
if ($old_value == $value) {
return;
}
// do stuff
}
I am trying to log each option update (later i will filter out the unnecessary ones). I have came across the 'update_option' and 'updated_option' hooks. They are triggering but the problem is that $old_value and $value are empty and/or not defined.
When I do not use default values for the $old_value and $value I get error that too few parameters was passed into the function. But according to WP documentation I should get both of them.
Any idea what´s wrong?
public function __construct()
{
add_action('update_option', [$this, 'update']);
}
/**
* @param $option string Name of the option to update
* @param $old_value mixed Old option value
* @param $value mixed New option value
*/
public function update($option, $old_value = "", $value = "")
{
/** @var $current_user WP_User */
global $current_user;
if ($old_value || $value) {
var_dump('old: ' . $old_value);
var_dump('new: ' . $value);
die;
}
if ($old_value == $value) {
return;
}
// do stuff
}
Share
Improve this question
edited Jul 26, 2019 at 12:41
Max Yudin
6,3882 gold badges26 silver badges36 bronze badges
asked Jul 26, 2019 at 11:23
Erik KubicaErik Kubica
1157 bronze badges
2
|
1 Answer
Reset to default 1The syntax should be: add_action('update_option', [$this, 'update'], 10, 3)
because your function is accepting 3 arguments:
public function __construct()
{
add_action('update_option', [$this, 'update'], 10, 3);
}
PS: add_action()
's reference.
add_action('update_option', [$this, 'update'], 10, 3)
because your function is accepting 3 arguments. – Sally CJ Commented Jul 26, 2019 at 11:55