Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#43379 closed feature request (wontfix)

Allow themes to know if they are the parent or child theme

Reported by: denvercoder9's profile denvercoder9 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords: close
Focuses: Cc:

Description

I've seen a few tickets from a long long time ago that would have potentially provided the functionality, however the solutions introduced were designed to solve completely different problems.

It would be convenient for a parent theme to know it's a parent or a child theme to know it's a child.

For example if I was to create a theme that was intended to support parent/child theme functionality but was also built around specific conventions.

As an example for a theme there is a config.php file that should be loaded for both paren/tchild, the configurations in this file would be different for each theme. Let's suppose there are a lot of conventions like this and I want the themes to automatically load different files without requiring the person setting up the child theme to update all instances of get_template_directory() with get_stylesheet_directory(). Maybe as another example is maybe the parent them provides a file for hooking into gravity forms if it exists. As a convention I would like the child theme to organize it's code in a similar way and specific file is used to for gravity forms hooks.

I think this could be solved in a variety of ways. Possibly providing a way to scope variables to a specific theme.

Or to create a utility function to identify if the current working theme is currently active. As I understand it code from a theme should only be running if it is a parent theme or a child theme. If the current working file is not in the active theme than it would inherently be a parent theme file.

While there is the ability to provide workarounds, the only ways I have identified often require expensive file system calls, which I'm hoping, with my limited knowledge of how core handles parent/child a solution could be introduced without these types of calls.

Change History (7)

#1 @swissspidy
7 years ago

  • Component changed from Customize to Themes
  • Summary changed from Allow themese to know if they are the parent or child theme to Allow themes to know if they are the parent or child theme

I'm not exactly sure what you want to achieve but you should be able to use something like this:

<?php
$active_theme = wp_get_theme();

$is_child_theme = (bool) $active_theme->parent()

#2 @denvercoder9
7 years ago

This would not work, this just lets me know if there is a child theme in use which case I could just use is_child_theme(). More so Im looking for something that would allow functionality in a theme to know if it's running as the parent or the child. For example a child would have the ability to say "am I a parent theme? no, ok I won't run x functionality" or "am I a parent theme? yes, ok I will use get_template_dir instead of get_stylesheet_dir"

In my example of loading files as a convention. If say for example I wanted to use specific glob patterns I could set a $base variable to build upon those patterns

<?php
$base = is_parent_theme() ? get_template_dir() : get_stylesheet_dir();

#3 @denvercoder9
7 years ago

I've previously attempted using a stateful object but the managment of this state can get tricky. Depending on how/when things are executing. While understandably the same complexity would possibly need to be implemented in core to provide similar functionality, I posted here in the event someone working on core might have further insight.

#4 @swissspidy
7 years ago

Ha, totally forgot about is_child_theme().

"am I a parent theme? no, ok I won't run x functionality" or "am I a parent theme? yes, ok I will use get_template_dir instead of get_stylesheet_dir"

So $is_parent_theme = ! is_child_theme()?

#5 @denvercoder9
7 years ago

Not quite, is_child_theme just lets you know if a child theme is in use if a child theme is in use it resolves true, if not it resolves false. In my use case is_child_theme would always resolve true because I'm looking for functionality specific to a parent/child environment.

I think my example clearly explains.. let me expand

Thefunctionality im looking for

<?php

//in both functions.php files (both as in parent and child)
$base = is_parent_theme() ? get_template_dir() : get_stylesheet_dir();

//in parent theme's functions.php
require_once($base . '/config/image_sizes.php);

//in child theme's functions.php
require_once($base . '/config/image_sizes.php);

As it exists presently you have to do...
parent theme's functions.php

<?php
$base = get_template_dir();
require_once($base . '/config/image_sizes.php);

child theme's functions.php

<?php
$base = get_stylesheet_dir();
require_once($base . '/config/image_sizes.php);

In this usecase I can prevent users cloning my theme for a child theme from having to update get_template_dir references to get_stylesheet_dir references. My example is extremely basic on purpose to prove a point. You could easily accomplish that specific functionality with DIR, but that's just because my example demonstrates the code in functions.php.

#6 @denvercoder9
7 years ago

  • Keywords close added
  • Resolution set to wontfix
  • Status changed from new to closed

As I've thought it over it probably doesn't make sense to do this. It could introduce a lot of code smell and hard to debug behavior. For example if you are calling parent theme methods from the child.

It's probably best to keep this type of functionality during bootstrap which can easily be accomplished via functions.php

I think it's safe to delete this as I don't think the original request makes sense.

#7 @SergeyBiryukov
7 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.