我从另一家开发公司得到的这个主题到处都是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);
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_filter
和add_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
不是模板,子主题不会更改方式
include
或
require
工作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 );
}
}
}