Make WordPress Core


Ignore:
Timestamp:
02/21/2024 07:24:12 PM (3 months ago)
Author:
joemcgill
Message:

Themes: Use original template paths when switching blogs.

This fixes a bug introduced by [57129] and [56635] in which deprecating the previous TEMPLATEPATH and STYLESHEETPATH constants in favor of get_template_directory() and get_stylesheet_directory() functions caused the active theme template path to change when using switch_to_blog().

This introduces a new function, wp_set_template_globals(), which is called during the bootstrap process to store the template paths to new globals values $wp_template_path and $wp_stylesheet_path. This restores behavior to how things worked prior to [56635] but retains the ability for template values to be reset for better testability.

Related #18298, #60025.

Props joemcgill, flixos90, mukesh27, swissspidy, manfcarlo, metropolis_john, jeremyfelt.
Fixes #60290.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/template.php

    r57223 r57685  
    681681
    682682/**
     683 * Set up the globals used for template loading.
     684 *
     685 * @since 6.5.0
     686 *
     687 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     688 * @global string $wp_template_path   Path to current theme's template directory.
     689 */
     690function wp_set_template_globals() {
     691    global $wp_stylesheet_path, $wp_template_path;
     692
     693    $wp_stylesheet_path = get_stylesheet_directory();
     694    $wp_template_path   = get_template_directory();
     695}
     696
     697/**
    683698 * Retrieves the name of the highest priority template file that exists.
    684699 *
     
    689704 * @since 2.7.0
    690705 * @since 5.5.0 The `$args` parameter was added.
     706 *
     707 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     708 * @global string $wp_template_path   Path to current theme's template directory.
    691709 *
    692710 * @param string|array $template_names Template file(s) to search for, in order.
     
    699717 */
    700718function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
    701     $stylesheet_path = get_stylesheet_directory();
    702     $template_path   = get_template_directory();
    703     $is_child_theme  = $stylesheet_path !== $template_path;
     719    global $wp_stylesheet_path, $wp_template_path;
     720
     721    if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
     722        wp_set_template_globals();
     723    }
     724
     725    $is_child_theme = is_child_theme();
    704726
    705727    $located = '';
     
    708730            continue;
    709731        }
    710         if ( file_exists( $stylesheet_path . '/' . $template_name ) ) {
    711             $located = $stylesheet_path . '/' . $template_name;
     732        if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
     733            $located = $wp_stylesheet_path . '/' . $template_name;
    712734            break;
    713         } elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) {
    714             $located = $template_path . '/' . $template_name;
     735        } elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
     736            $located = $wp_template_path . '/' . $template_name;
    715737            break;
    716738        } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
Note: See TracChangeset for help on using the changeset viewer.