Replacing the parent action of a Custormizr theme

I created my own child theme. Everything works fine, except that I cannot unregister.

$ is the TC_footer_main class, and the following code is in __construct

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

I tried to delete some actions without success. I am just trying to change / delete the footer:

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

or

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

I also tried

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

But this caused an error because TC_footer_main not loaded at the time my functions.php file was launched.

+5
source share
3 answers

I am just trying to change / delete the footer:

I think you make it more complicated to change the output of the tc_colophon_center_block() method than it should be.

Just use the tc_credits_display filter:

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

to change this unit to suit your needs.

To completely remove the output (if allowed), simply use:

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

You have access to filters like:

  • tc_copyright_link
  • tc_credit_link
  • tc_wp_powered

to choose from.

What is it!

+3
source

For your purpose, add the following code to function.php. It will receive an on_setup_theme hook call.

 // 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' ); 

You can also try overriding the parent class from the child class, as described here: https://thethemefoundry.com/tutorials/advanced-customization-replacing-theme-functions/

+3
source

you're too far away ... one of the problems that may arise is that you are trying to remove the hook before it is added by your parent theme. The class can be initialized at a later stage ...

im not too sure when your hook is working, but hopefully after init

  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 } 

now you can just add an action for your new function.

There is a note that an anonymous function has been added, the &$this value is often ignored when trying to delete a connected function. This is a pain because wp will assign a random string as the key name and function name for this function, each time it is not guessed. But we can look for the function name inside the key, so something like this will work

  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'); }); 
+2
source

Source: https://habr.com/ru/post/1241655/