plugin development - hook update_optionupdated_option empty $old_value and $value

I am trying to log each option update (later i will filter out the unnecessary ones). I have came across the 'updat

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
  • 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. – Erik Kubica Commented Jul 26, 2019 at 12:36
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293328a4620993.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信