0

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
}
2
  • 2
    The syntax should be: add_action('update_option', [$this, 'update'], 10, 3) because your function is accepting 3 arguments.
    – Sally CJ
    Commented Jul 26, 2019 at 11:55
  • Oh, how can I be so stupid. Thank you very much. If you will post it as an answer i will accept it. Commented Jul 26, 2019 at 12:36

1 Answer 1

1

The 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.

1
  • 1
    Thank you again, you answer is now accepted. Commented Jul 26, 2019 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.