替换 Custormizr 主题的父操作

Replacing parent action of Custormizr theme

我创建了自己的儿童主题。一切都很好,除了我似乎无法注销钩子。

$这是classTC_footer_main而下面的代码在__construct

add_action ( '__colophon'       , array( $this , 'tc_colophon_center_block' ), 20 );

我尝试了几个删除操作但没有成功。我只是想 change/remove 页脚:

remove_action( '__colophon' , 'tc_colophon_center_block' , 55);

remove_action( '__colophon' , array('TC_footer_main','tc_colophon_center_block') , 55);

我也试过了

remove_action( '__colophon' , TC_footer_main::$instance->tc_colophon_center_block() , 55);

但这引发了一个错误,因为 TC_footer_main 在我的 functions.php 文件 运行 时没有加载。

为了您的目的,在 function.php 中添加以下代码。它将在 after_setup_theme 挂钩上得到调用。

// replace parent function
function child_theme_function () {
    // your code goes here
 }

function my_theme_setup () {
    remove_action( '__colophon', 'tc_colophon_center_block', 1000 );
    add_action( '__colophon', 'child_theme_function', 1000 );
}
add_action( 'after_setup_theme', 'my_theme_setup' );

您也可以尝试从子 class 覆盖父 class,如下所述:https://thethemefoundry.com/tutorials/advanced-customization-replacing-theme-functions/

I am just trying to change/remove the footer:

我认为您修改 tc_colophon_center_block() 方法的输出比它必须的要复杂得多。

只需使用 tc_credits_display 过滤器:

add_filter( 'tc_credits_display', function( $html )
{
    // Modify output to your needs!
    return $html;
} );

根据您的需要修改该块。

要完全删除输出(如果允许的话),只需使用:

add_filter( 'tc_credits_display', '__return_null', PHP_INT_MAX );

您可以进一步访问以下过滤器:

  • tc_copyright_link
  • tc_credit_link
  • tc_wp_powered

可供选择。

就是这样!

你离得太远了......你可能遇到的一个问题是你试图在你的父主题添加钩子之前删除钩子.. class 可以在稍后阶段初始化...

我不太确定你的钩子何时运行,但希望它在初始化之后运行

 add_action('init', 'remove_parent_hook');

 function remove_parent_hook(){
      remove_action( '__colophon' , array('TC_footer_main','tc_colophon_center_block') , 20); // needs to be the same priority
 }

您现在显然可以为新功能添加一个操作。

有可能添加了匿名函数,在尝试删除挂钩函数时,往往会忽略 &$this 的重要性。这很痛苦,因为 wp 会分配一个随机字符串作为函数的键名和函数名,每次都不同,所以无法猜测。但是我们可以在键中搜索函数名称,这样就可以了

    function remove_anon_hk($hook, $function, $priority=10 ){

        global $wp_filter;

        $hooks= $wp_filter[$hook][$priority];

        if(empty ($hooks))
            return;

        foreach($hooks as $hk=>$data):
            if(stripos($hk, $function) !== false ){
                unset($wp_filter[$hook][$priority][$hk]);
            }
        endforeach;
    }


    add_action('init', function(){
        remove_anon_hk('__colophon', 'tc_colophon_center_block');
    });