0

I understand the following about functions.php:

Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)

The problem with this logic is that things, in my framework, are loaded via action hooks in some cases. so for example you can turn off the admin options in child themes by remove_action()

How ever with the logic presented above, That action that loads the admin options is not technically loaded until after the child themes functions.php.

So the action is never created nor is it's associated function technically added. so has_action() will always return false in a child theme. (or at least it is for me in this instance).

What do I need? I need the parent themes functions.php to load FIRST before the child themes. or at least to be first loaded object.

Some people have suggested to do require parent theme function before doing anything in a child themes functions.php.

is this the right way to handle this situation? where you need the parent themes functions.php to load before the child?

1 Answer 1

3

The answer is to remove things in your child theme via an action that runs before the action the parent theme has hooked to, everything should be happening within an action of some sort.

For example, in your parent theme:

function do_something(){
    // something happens here
}
add_action( 'init', 'do_something' );

Then in your child theme:

function check_something(){
    remove_action( 'init', 'do_something' );
    add_action( 'init', 'do_my_own_thing' );
}
add_action( 'after_setup_theme', 'check_something' );

Or another example, in parent theme:

function some_func(){
    add_action( 'admin_menu', 'do_admin_things' );
}
add_action('init', 'some_func');

function do_admin_things(){
    // admin things
}

Then in the child theme, hook into init with a later priority:

function check_admin_things(){
    remove_action( 'admin_menu', 'do_admin_things' );
}
add_action( 'init', 'check_admin_things', 100 );
3
  • I see your adding everything to init, this doesn't seem right (in all cases). what about custom hooks, such as add_action('bla', 'function'); do_action('bla')?
    – TheWebs
    Commented Mar 16, 2013 at 4:56
  • it would work the same way. in the first example you'd remove the action in the child theme at after_setup_theme. at that point, your parent theme has added the action.
    – Milo
    Commented Mar 16, 2013 at 5:12
  • 1
    Note that you need to make sure that the priority of your child action is higher than that of the parent action if both actions are on the same hook. So for instance if trying to override twentytwelve_setup your override also has to be on after_setup_theme and you need to hook in with a higher priority to make sure your override kicks in before the original eg // Remove the default 2012 setup function function remove_twentytwelve_actions() { remove_action( 'after_setup_theme', 'twentytwelve_setup' ); } add_action('after_setup_theme','remove_twentytwelve_actions', 0);
    – benz001
    Commented Apr 4, 2013 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.