Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 );
share|improve this answer
    
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 Mar 16 '13 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 Mar 16 '13 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 Apr 4 '13 at 5:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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