【问题标题】:Modify a plugin without changing it修改插件而不更改它
【发布时间】:2021-09-04 05:06:24
【问题描述】:

我从插件中复制了下面的代码,我只想在数组中添加另一个选项,例如“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',
                )
            );
        }
    }

【问题讨论】:

    标签: arrays wordpress filter plugins


    【解决方案1】:

    由于有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/

    【讨论】:

    • @TALHA,如果此答案帮助您考虑标记为正确答案。
    猜你喜欢
    • 2018-10-30
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多