修改插件而不更改它

Modify a plugin without changing it

我从插件中复制了以下代码,我只想在数组中添加另一个选项,如 'option4',而不更改插件中的代码。我想使用子主题,这样每当插件更新时我的选项就不会被删除。

class my_class {
        private $var;
        protected function __construct() {
            add_action( 'init', array( $this, 'register' ) );
        }
        public function register() {
            $this->var= apply_filters(
                'custom_plugin',
                array(
                    'option1' => 'value1',
                    'option2' => 'value2',
                    'option3' => 'value3',
                )
            );
        }
    }

因为有一个apply_filter你可以改变它如下:

// use the filter name to add_filter
add_filter( 'custom_plugin', 'add_value_to_array' );

// Callback function which is getting the array value as its paramter.
function add_value_to_array( $array) {
    // Change/Add/Apend the value
    $array['option4'] = 'value4';
    // return it.
    return $array;
}

如果你想详细了解它,我找到了一个你可以浏览的博客:https://wpshout.com/apply_filters-do_action/