删除使用CREATE_Function()设置的操作/过滤器

时间:2018-12-20 作者:Nick M

我从另一家开发公司得到的这个主题到处都是add_action/filter( \'hook/filter\', create_function( \'\', \'return blah(\'blah\')\' ); 我们将此站点移动到运行php 7.2.

我想知道删除这些操作和过滤器的合适方法是什么。或者我可以简单地将/path/to/file复制并粘贴到子目录中,然后知道这将被覆盖吗?

这里有一个例子

if ( ! function_exists( \'ot_register_theme_options_page\' ) ) {

        function ot_register_theme_options_page() {

            /* get the settings array */
            $get_settings = _ut_theme_options();

            /* sections array */
            $sections = isset( $get_settings[\'sections\'] ) ? $get_settings[\'sections\'] : [];

            /* settings array */
            $settings = isset( $get_settings[\'settings\'] ) ? $get_settings[\'settings\'] : [];

            /* build the Theme Options */
            if ( function_exists( \'ot_register_settings\' ) && OT_USE_THEME_OPTIONS ) {

                ot_register_settings(
                    [
                        [
                            \'id\'    => \'option_tree\',
                            \'pages\' => [
                                [
                                    \'id\'              => \'ot_theme_options\',
                                    \'parent_slug\'     => apply_filters( \'ot_theme_options_parent_slug\', \'unite-welcome-page\' ),
                                    \'page_title\'      => apply_filters( \'ot_theme_options_page_title\', __( \'Theme Options\', \'option-tree\' ) ),
                                    \'menu_title\'      => apply_filters( \'ot_theme_options_menu_title\', __( \'Theme Options\', \'option-tree\' ) ),
                                    \'capability\'      => $caps = apply_filters( \'ot_theme_options_capability\', \'edit_theme_options\' ),
                                    \'menu_slug\'       => apply_filters( \'ot_theme_options_menu_slug\', \'ut_theme_options\' ),
                                    \'icon_url\'        => apply_filters( \'ot_theme_options_icon_url\', null ),
                                    \'position\'        => apply_filters( \'ot_theme_options_position\', null ),
                                    \'updated_message\' => apply_filters( \'ot_theme_options_updated_message\', __( \'Theme Options updated.\', \'option-tree\' ) ),
                                    \'reset_message\'   => apply_filters( \'ot_theme_options_reset_message\', __( \'Theme Options reset.\', \'option-tree\' ) ),
                                    \'button_text\'     => apply_filters( \'ot_theme_options_button_text\', __( \'Save Changes\', \'option-tree\' ) ),
                                    \'screen_icon\'     => \'themes\',
                                    \'sections\'        => $sections,
                                    \'settings\'        => $settings,
                                ],
                            ],
                        ],
                    ]
                );

                // Filters the options.php to add the minimum user capabilities.
                add_filter( \'option_page_capability_option_tree\', create_function( \'$caps\', "return \'$caps\';" ), 999 );

            }

        }

    }
我知道add_filter 会变成现实add_filter( \'option_page_capability_option_tree\', function($caps) { return $caps; }, 999);

2 个回复
SO网友:Tom J Nowell

为了删除挂钩和过滤器,我们需要能够获取对添加的可调用项的引用,但在这种情况下,create_function 使用了,所以这非常困难。这也是您收到弃用通知的原因。遗憾的是,修复这些不赞成意见的唯一方法是更改父主题来修复它。

修复不赞成意见的PHP文档说create_function 工作原理如下:

string create_function ( string $args , string $code )
根据传递的参数创建匿名函数,并为其返回唯一名称。

值得注意的是,create_function 在PHP 7.2中不推荐使用

因此:

create_function( \'$a\', \'return $a + 1;\' )
成为:

function( $a ) {
    return $a + 1;
}
甚至

function add_one( $a ) {
    return $a + 1;
}
这应该消除create_function 调用,并将匿名函数转换为可以在子主题中删除的普通调用

删除挂钩和过滤器现在我们已经完成了,现在可以在子主题中删除并替换它们。

为此,我们需要两件事:

运行删除代码after 添加这些过滤器是为了能够解开过滤器/操作,更换起来很容易,只需使用add_filteradd_action, 但这会添加您的版本,我们需要删除父主题版本。

假设此代码位于父主题中:

add_filter(\'wp_add_numbers_together\' \'add_one\' );
我们想用multiply_by_two 作用首先,让我们在init 挂钩:

function nickm_remove_parent_hooks() {
    //
}
add_action(\'init\', \'nickm_remove_parent_hooks\' );
然后打电话remove_filter and 添加\\u筛选器“”:

function nickm_remove_parent_hooks() {
    remove_filter( \'wp_add_numbers_together\' \'add_one\' );
}
add_action( \'init\', \'nickm_remove_parent_hooks\' );
add_action( \'wp_add_numbers_together\', \'multiply_by_two\' );
请记住,子主题可以覆盖模板,但是functions.php 不是模板,子主题不会更改方式includerequire 工作A儿童主题functions.php 首先加载,但在其他方面,它们类似于插件。

最后,你不应该在主题中注册帖子类型、角色、功能和分类法,这是一个巨大的危险信号,会造成锁定,阻止数据的可移植性。这些东西应该放在插件中,而不是主题中

SO网友:Nick M

因此,采取以下措施if ( ! function_exists( \'ot_register_theme_options_page\' ) ) { } 调整过滤器将优先于父主题。

if ( ! function_exists( \'ot_register_theme_options_page\' ) ) {

    function ot_register_theme_options_page() {

        /* get the settings array */
        $get_settings = _ut_theme_options();

        /* sections array */
        $sections = isset( $get_settings[\'sections\'] ) ? $get_settings[\'sections\'] : [];

        /* settings array */
        $settings = isset( $get_settings[\'settings\'] ) ? $get_settings[\'settings\'] : [];

        /* build the Theme Options */
        if ( function_exists( \'ot_register_settings\' ) && OT_USE_THEME_OPTIONS ) {

            ot_register_settings(
                [
                    [
                        \'id\'    => \'option_tree\',
                        \'pages\' => [
                            [
                                \'id\'              => \'ot_theme_options\',
                                \'parent_slug\'     => apply_filters( \'ot_theme_options_parent_slug\', \'unite-welcome-page\' ),
                                \'page_title\'      => apply_filters( \'ot_theme_options_page_title\', __( \'Theme Options\', \'option-tree\' ) ),
                                \'menu_title\'      => apply_filters( \'ot_theme_options_menu_title\', __( \'Theme Options\', \'option-tree\' ) ),
                                \'capability\'      => $caps = apply_filters( \'ot_theme_options_capability\', \'edit_theme_options\' ),
                                \'menu_slug\'       => apply_filters( \'ot_theme_options_menu_slug\', \'ut_theme_options\' ),
                                \'icon_url\'        => apply_filters( \'ot_theme_options_icon_url\', null ),
                                \'position\'        => apply_filters( \'ot_theme_options_position\', null ),
                                \'updated_message\' => apply_filters( \'ot_theme_options_updated_message\', __( \'Theme Options updated.\', \'option-tree\' ) ),
                                \'reset_message\'   => apply_filters( \'ot_theme_options_reset_message\', __( \'Theme Options reset.\', \'option-tree\' ) ),
                                \'button_text\'     => apply_filters( \'ot_theme_options_button_text\', __( \'Save Changes\', \'option-tree\' ) ),
                                \'screen_icon\'     => \'themes\',
                                \'sections\'        => $sections,
                                \'settings\'        => $settings,
                            ],
                        ],
                    ],
                ]
            );

            // Filters the options.php to add the minimum user capabilities.
            add_filter( \'option_page_capability_option_tree\', create_function( \'$caps\', "return \'$caps\';" ), 999 );

        }

    }

}

相关推荐

EDIT-Comments.php COMMENT_ROW_ACTIONS AJAX问题

在编辑注释上。php页面,我正在连接到comment\\u row\\u actions过滤器,以便在末尾添加另一个链接。我正在复制“Approve”链接,添加另一个查询键和值,并修改锚文本。我想根据新查询键的值添加一些注释元。批准的链接:/wp-admin/comment.php?c=9999&action=approvecomment&_wpnonce=8526c66已批准的元链接/wp-admin/comment.php?c=9999&action=approvecommen