How to load parent_theme functions.php before child_theme?

The question:

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!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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() {
...
}

Method 2

Firstly, you can’t. The child theme’s functions.php always loads first, period.

Second, themes can’t hook to setup_theme. Plugins can, but the first thing a theme can hook to is after_setup_theme.

If your parent is designed correctly, then the child is capable of overriding functions and stuff in the parent, but only when it loads first.

Broadly speaking, if you think you need to load the parent’s functions file first, then you’re probably doing it wrong, somehow. You need to explain the larger issue.

Method 3

So you’re trying to execute code from the child’s functions.php, but after the parent theme has loaded. Simple, just use a custom action:

At the end of parent/functions.php:

do_action('parent_loaded');

In child/functions.php:

function parent_loaded() {
    // do init stuff
}
add_action('parent_loaded', 'parent_loaded');

All parent themes worth their salt do it this way. What’s more, they have several other actions and filters sprinkled around for the child theme to use.

Method 4

Why don’t you include parent theme’s functions.php file in child theme’s functions.php file, like this:

In child theme’s functions.php file:

if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( TEMPLATEPATH . '/functions.php' ) )
            include( TEMPLATEPATH . '/functions.php' );

// code of child theme's functions.php file continues here

By this way, the parent theme’s functions.php file is not changed (sometimes this is important).

Method 5

I had a similar issue before and I fix it by creating an a extra empty “child-functions.php” in parent and including into “functions.php” (parent too) right after the files / functions I want to use in child , then in child theme I create a “child-functions.php” file and there is where I can start simulate child functions.php run after parent isn’t an a elegant solution but did the work.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment