Wordpress - How to load parent_theme functions.php before child_theme?
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.
- As far as I remember the trick is to wrap your filters and actions into it's function.
See example below. - You do that in both parent and child
functions.php
files. - 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() {
...
}
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.
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.