Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 436
c3cris Avatar asked Jan 27 '16 20:01

c3cris


People also ask

Should I activate child theme or parent theme?

Parent themes will usually erase any customizations you've made to them when they're updated. Child themes enable you to keep your customizations separate from your base theme, and intact after updates. Any changes you make to your child theme can easily be reversed by simply disabling it.

What is the difference between child theme and parent theme in WordPress?

➜ A parent theme is independent of any other theme for working while a child theme is dependent on the parent theme to work. ➜ A child theme can save you from losing your customizations even if you update the parent theme as all the customizations are stored in the child theme and you can view or edit them there.

How do I add a parent theme to a child theme in WordPress?

Like any theme, child themes are located in wp-content/themes in your WordPress installation. So, navigate there now and create a new folder for your child theme. A best practice is to give your theme's folder the same name as the parent theme and append it with -child .


2 Answers

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!

like image 114
birgire Avatar answered Sep 30 '22 09:09

birgire


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/

like image 28
PHPExpert Avatar answered Sep 30 '22 10:09

PHPExpert