Replacing parent action of Custormizr theme

I created my own child theme. Everything works great except I can’t seem to unregister a hoook.

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

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

I tried several remove actions with no success. I am just trying to change/remove the footer:

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

or

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

I’ve also tried

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

But that threw an error as TC_footer_main was not loaded by the time my functions.php file ran.

Related posts

3 comments

  1. I am just trying to change/remove the footer:

    I think you’re making it way more complex, to modify the output of the tc_colophon_center_block() method, than it has to be.

    Just use the tc_credits_display filter:

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

    to modify that block to your needs.

    To totally remove the output (if that’s allowed), simply use:

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

    You have further access to filters like:

    • tc_copyright_link
    • tc_credit_link
    • tc_wp_powered

    to choose from.

    That’s it!

  2. For your purpose add the following code in function.php. It will get call on after_setup_theme hook.

    // 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 for overriding the parent class from child class as described here: https://thethemefoundry.com/tutorials/advanced-customization-replacing-theme-functions/

  3. you werent too far away…one problem you may have is you are trying to remove the hook before it has been added by your parent theme..the class could be initialized at a later stage…

    im not too sure when your hook runs, but hopefully its 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
     }
    

    you can obviously now just add a action for your new function.

    There is a offchance that a anonymous function has been added, often the significance of &$this is overlooked when trying to remove a hooked function. This is a pain because wp will assign a random string as the key name & the function name for the function, its different every time so it cant be guessed. But we can search for the function name within 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');
        });
    

Comments are closed.