WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 4 months ago

#13084 closed defect (bug) (wontfix)

Loading Parent Theme functions BEFORE Child Theme functions

Reported by: oncletom Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9.2
Component: Themes Keywords:
Focuses: Cc:

Description

Hello,

I am creating a child theme for a mobile purpose.
The goal was to complete parent theme's functions.php and modify already configured behavior from the child theme to improve things.

It would be more logical that child theme's functions.php be loaded AFTER parent theme's one... like it is done for template file overloading.

The parent theme is not aware of the child one.
The opposite is true.

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

should be replaced by

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

in wp-settings.php

Patch is attached.

Attachments (1)

wp-settings.patch (756 bytes) - added by oncletom 6 years ago.

Download all attachments as: .zip

Change History (9)

#1 @demetris
6 years ago

  • Cc dkikizas@… added

One reason it’s done this way is to allow parent themes to make their functions pluggable by declaring them conditionally. E.g., the parent says:

if (!function_exists('mega_clever_function')) {
    function mega_clever_function() {
        Do mega clever stuff!
    }
}

Then the child comes along and does not like mega_clever_function. So it simply does:

function mega_clever_function() {
    Do different clever stuff!
}

You can see examples in the functions.php of the new default theme, Twenty Ten. (But that order of loading the two files was there long before 2010.)

#2 @greenshady
6 years ago

There were times when I wished the files were loaded in this order, but it's too late in the game to make this change. This is how it's been done for years now. I imagine there'll be some backwards-compatibility issues.

Assuming your parent theme's functions.php is coded well, it shouldn't be tough to override its functionality even if it's loaded last.

#3 @dd32
6 years ago

  • Milestone Unassigned deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I agree with most of the points above, I've wanted it to happen the other way around a few times, but ultimately, theres a reason it was made the way it is, and to change it now would break a lot of themes.. with little benefit.

#4 follow-up: @nacin
6 years ago

It's also very easy for a child theme to allow the parent theme functions to be loaded first, by simply delaying execution of anything until the after_setup_theme hook.

#5 in reply to: ↑ 4 ; follow-up: @sidati
4 months ago

  • Keywords changed from parent, child, theme to parent child theme
  • Resolution wontfix deleted
  • Status changed from closed to reopened

@nacin But if we want to use "heritability" like in OOP, for example this is not possible for the moment

<?php

class ParentTheme {
        function __construct(){
                add_action('action_one', array($this, 'function_one'));
                add_action('action_two', array($this, 'function_two'));
                add_action('action_three', array($this, 'function_three'));
        }

        function function_two() {
                return 2;
        }
}

class ChildTheme extends ParentTheme {

        function function_two() {
                return 9;
        }

}

@greenshady Why not ? the child theme functions file is independent since its not related to parent function file, clearly because its called before it and the parent theme is developed before the child so clearly it will not counting on the child theme, so changing order will not make any difference.

#6 in reply to: ↑ 5 ; follow-up: @greenshady
4 months ago

Replying to sidati:

@greenshady Why not ? the child theme functions file is independent since its not related to parent function file, clearly because its called before it and the parent theme is developed before the child so clearly it will not counting on the child theme, so changing order will not make any difference.

It'll mean that any parent theme pluggable functions that have been overwritten in a child theme will cause a fatal error when the change is made. Fatal errors are a showstopper. That's why you could never change the load order.

It'll also mean that any actions and filters (those with the same set priority) will now be executed first in the parent theme and last in the child theme, which will cause some issues for some. Not as bad as the fatal error though.

#7 in reply to: ↑ 6 @sidati
4 months ago

Replying to greenshady:

Replying to sidati:
It'll mean that any parent theme pluggable functions that have been overwritten in a child theme will cause a fatal error when the change is made. Fatal errors are a showstopper. That's why you could never change the load order.

OK, Maybe we can introduce a CONSTANT in wp-config.php like define('PARENT_FUNCTIONS_FIRST', false); or maybe option/theme-mode for that.

I know its not very URGENT or IMPORTANT request, but believe me its very useful and we will not break anything since the default value still child's functions.php first, what you think ?

#8 @dd32
4 months ago

  • Keywords parent child theme removed
  • Resolution set to wontfix
  • Status changed from reopened to closed

This is still a wontfix unfortunately. We just can't alter the loading order, no matter how much you want to. Adding a constant or feature flag isn't an option either.

If you wish to use the parent themes classes in the child theme, you simply have to delay code execution until the after_setup_theme action, for example:

add_action( 'after_setup_theme', function() {
  include __DIR__  . '/class-child-theme-extends-parent-theme-class.php';
  $child_theme = new Child_Theme_Class();
} );
Note: See TracTickets for help on using tickets.