[wp-trac] [WordPress Trac] #52276: Feature Request - additional hooks for parent and child theme
WordPress Trac
noreply at wordpress.org
Tue Jan 12 07:36:29 UTC 2021
#52276: Feature Request - additional hooks for parent and child theme
-----------------------------+------------------------------
Reporter: landwire | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version: 5.6
Severity: normal | Resolution:
Keywords: | Focuses:
-----------------------------+------------------------------
Comment (by landwire):
Thanks. I have not worked with pluggable functions yet. I usually work
with namespaces and classes in PHP, so I do not get any conflicts. I
understand why you do not want to switch the load order, also with respect
to how the templates are loaded. That makes sense.
But if I want to extend a class that the parent defines that is not
possible? Is that correct?
I.e: inside the child theme I would like to use:
{{{
use Parent\Utils\FileLoader;
}}}
I could only get this to work with the earliest hook after the parent
theme is loaded like so:
{{{
add_action(
'after_setup_theme',
function () {
$composer_autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($composer_autoload)) {
require_once $composer_autoload;
}
ChildFileLoader::loadPhpFiles('inc'); // needs to stay here as it
loads class below
new ChildSite();
}
);
}}}
But then I had problems that "known hooks" to remove parent functionality
would not work at all in the child theme like the below. It would be a
pain to change all those actions to something later like "init" or
whatever as they are used in the parent theme with "after_setup_theme" and
often code is copy and pasted.
{{{
add_action('after_setup_theme', 'Child\Gutenberg\addGutenbergAssets');
function removeGutenbergFromParentTheme()
{
remove_action('after_setup_theme',
'Parent\Gutenberg\addGutenbergAssets');
}
}}}
Adding the 2 additional hooks, when a child theme is used, would help
circumvent that problem.
The below "after_setup_parent_theme" would fire just before
"after_setup_theme", so that "after_setup_theme" actions in the child
theme would still work as expected.
{{{
add_action(
'after_setup_parent_theme',
function () {
$composer_autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($composer_autoload)) {
require_once $composer_autoload;
}
ChildFileLoader::loadPhpFiles('inc'); // needs to stay here as it
loads class below
new ChildSite();
}
);
}}}
Just so you know the reason why I am suggesting those hooks.
In the end it will just be about 10 lines of code in wp-settings.php, that
will make the use of parent and child themes a bit more flexible,
especially if you are not using pluggable functions, but rather extending
classes. (Pluggable) functions are not really that easy to extend and
often you end up with nearly the same/just slightly different code in the
child function. Then you have to maintain the code in two places.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/52276#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list