简体   繁体   中英

Override function (with hook) in wordpress

I have this situation:

parent theme:

functions.php

require_once(dirname(__FILE__) . "/includes/theme-functions.php");
require_once(dirname(__FILE__) . "/includes/theme-hooks.php"); 

theme-function.php

function name_of_this_function() {
    // DO SOMETHING
}
add_action ('hook','name_of_this_function');

theme-hooks.php

function hook() {
    do_action('hook');
}

How can I override that action in my child functions.php given that mine is called before? I tried:

remove_action ('hook','name_of_this_function');

but of course this return false

I came to an answer after many attempts (miserably failed) and a lot of reading of documentation online.

The solution resides in the "after_setup_theme" hook.

Before I was adding my actions "on top" of the parent's, meaning that i had two headers, two footers and so on. Of course the problem was that my functions.php was not able to remove something that was not added yet.

This was caused by wordpress behavior that runs functions.php of the child before the one of the parent theme.

The solution to this problem is to add a function after the setup of theme with a priority that force it to runs after the one of the parent. In this way ALL functions of the parent would be initialized (the after setup of parent is called as last function) and I would be able to remove some actions. This is the syntax:

add_action('after_setup_theme', 'my_setup', 3);
function my_setup(){
    /* CUSTOM FOOTER */
    remove_action('footer_hook','parent_site_info',99);
    add_action('footer_hook','site_info',99);
}

That solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM