I got the problem that i have to load my parent theme’s functions.php file before my child theme’s functions.php file loads. This is needed for the setup & init procedure. I looked at the hooks inside /wp_core_root/wp-settings.php (named: do_action('setup_theme');).

The problem is that i don’t know how to hook into there, because the first file i get is the child theme’s functions.php, so no add_action( 'setup_theme', 'my_init_function' ); will work.

Edit:
a) I know that plugins load earlier than theme and therefore can access even the initial Query, but i don’t want to rely on a Plugin.
b) Here’s the code (shortened) from wp-settings.php file

// happens a lot earlier:  
do_action( 'plugins_loaded' );

// localize stuff happening here
    do_action( 'setup_theme' );

        // Load the functions for the active theme, for both parent and child theme if applicable.
        if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
            include( STYLESHEETPATH . '/functions.php' );
        if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
            include( TEMPLATEPATH . '/functions.php' );
    // first available hook, *after* functions.php was loaded
    do_action( 'after_setup_theme' );

I want to avoid two things: First a lot of explanation to users. Second the chance that someone breaks anything if the cuts the rope with accidently deleting the parents init procedure. People shall just play inside the functions.php without risking to break anything without knowing it.

In other words: How do i keep my child themes functions.php file clean, but have the the parent themes bootstrap done?

Any ideas?
Thanks a lot!

5

Justin Tadlock recently wrote a great post about making a better functions.php file
where (if I remember correctly) he deals with this exact issue.

Unfortunately his site is down at the moment so I have to rely on my memory for now.

You are on the right track with the after_setup_theme hook.

  1. As far as I remember the trick is to wrap your filters and actions into it’s function.
    See example below.
  2. You do that in both parent and child functions.php files.
  3. Then you can play with the priority of these two hooks.

Little bit of code worth thousand words – your parent theme’s function.php should look like this:

add_action( 'after_setup_theme', 'your_parent_theme_setup', 9 );
function your_parent_theme_setup() {    
    add_action(admin_init, your_admin_init);
    add_filter(the_content, your_content_filter);
}

function your_admin_init () {
...
}

function your_content_filter() {
...
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *