WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 6 months ago

#22355 new enhancement

Template stack - Beyond parent/child theme relationships — at Version 7

Reported by: johnjamesjacoby Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch
Focuses: Cc:

Description (last modified by johnjamesjacoby)

Problem

Robust plugins (BuddyPress, bbPress, et all) are unable to break out of the parent/child restrictions that WordPress imposes on template output. For these plugins to include their own template parts, elaborate code must be written to hook into several different execution points, requiring an intimate knowledge of the inner workings of WordPress's template loading system.


Solution

Create a stack of template locations, and allow WordPress to transverse this array following the same STYLESHEETPATH/TEMPLATEPATH order it always has, while also enabling additional paths to be added with a priority (similar to the filters API.)


Details

The attached patch includes two new functions in wp-includes/template.php:

  • register_template_stack()
  • get_template_stack()

Register template stack is a wrapper for the 'template_stack' filter. get_template_stack() is a variation of apply_filters() that returns the array of filtered template locations.

A modification to wp-settings.php calls register_template_stack() two times, passing get_stylesheet_directory() and get_template_directory() as callbacks, to initialize the core parent/child relationship, ensuring complete backwards compatibility.


Result

This allows for plugins to register additional paths in the template loader hierarchy, and enables plugins that may come with their own default template parts the option of registering a fallback template location.

This works with both locate_template() and get_template_part(), and has the added benefit removing duplicate items inside of get_template_stack(), resulting in avoiding an additional file system check should the parent and child themes be the same.

Change History (8)

@johnjamesjacoby3 years ago

comment:1 @obenland3 years ago

  • Cc obenland added

comment:2 @JustinSainton3 years ago

  • Cc justinsainton@… added

comment:4 @curtismchale3 years ago

  • Cc curtis@… added

comment:5 @mordauk3 years ago

  • Cc pippin@… added

comment:6 @MikeSchinkel3 years ago

  • Cc mike@… added

comment:7 @johnjamesjacoby3 years ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.