Wordpress remove_action 在子主题中不工作

Wordpress remove_action not working in child theme

我在 Wordpress 中使用 Unite 主题和我创建的子主题。

尝试删除操作时,php 错误表明子主题中的 functions.php 文件在父主题 /inc/extras.php 之前加载,其中包含我想覆盖的钩子。

尝试正确地做所有事情,而不是仅仅在父主题中进行快速而肮脏的更改。

父级 /inc/extras.php 从包含在父级 functions.php;

中加载
add_action('woocommerce_before_main_content', 'unite_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'unite_wrapper_end', 10);

function unite_wrapper_start() {
  echo '<div id="primary" class="col-md-8">';
}

function unite_wrapper_end() {
  echo '</div>';
}

当我将删除添加到同一个文件时,它起作用了;

add_action('woocommerce_before_main_content', 'unite_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'unite_wrapper_end', 10);

function unite_wrapper_start() {
  echo '<div id="primary" class="col-md-8">';
}

function unite_wrapper_end() {
  echo '</div>';
}

remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);

然而,当我添加到子 functions.php 文件时,它没有;

remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);

子 functions.php 文件当然正在加载和工作...

为什么会这样?

add_action('init' , 'remove_functions' , 15 );
function remove_functions() {
 remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);
}
add_action('woocommerce_before_main_content', 'unite_wrapper_start1', 10);
function unite_wrapper_start1(){
 echo '<div id="primary" class="col-md-12">';
}

与其说是另一个答案,不如说是对上述斯图尔特答案的补充。

这可用于删除父主题自定义(动态 'inc' 文件),如 Bard 这样的主题,其中父 functions.php 'requires' 一个 'inc' 文件包含进一步的功能和操作 (bard_dynamic_css).

1) 父函数文件读取:

require get_parent_theme_file_path( '/inc/customizer/customizer-defaults.php' );

2) 在子函数文件中:

add_action('init' , 'remove_functions' , 15 );
 function remove_functions() {
 remove_action( 'wp_head', 'bard_dynamic_css',10);
}

3) 然后要求子动态css

require get_stylesheet_directory() . '/inc/customizer/dynamic-css.php';

记住:更改原始函数名称(在子 'dynamic-css.php' 中)以避免冲突,例如'bard_dynamic_css' 至 'child_bard_dynamic_css'