[wp-trac] [WordPress Trac] #58866: Avoid consts inside `locate_template` as it gives incorrect template path after switch_theme

WordPress Trac noreply at wordpress.org
Thu Jul 20 17:59:21 UTC 2023


#58866: Avoid consts inside `locate_template` as it gives incorrect template path
after switch_theme
--------------------------+-----------------------------
 Reporter:  thekt12       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  assigned
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:  template      |
--------------------------+-----------------------------
 To replicate

 Add this following code in any active plugin -
 {{{#!php
 <?php
 function test_localtest_template() {
         print_r("<pre>");
         $current_theme = wp_get_theme()->stylesheet;
         print_r( "<br>Current theme :". $current_theme );
         print_r( "<br>path of current style.css asper locate_template : ".
 locate_template( 'style.css' ) );
         $themes = wp_get_themes();
         unset( $themes[$current_theme] );
         $switch_to_theme = array_pop($themes)->stylesheet;
         print_r( "<br>Switching to :". $switch_to_theme );
         switch_theme( $switch_to_theme );
         print_r( "<br>Theme after switching ". wp_get_theme()->stylesheet
 );
         print_r( "<br>path of new style.css asper locate_template : ".
 locate_template( 'style.css' ) );
         print_r("</pre>");
         die();
 }
 add_action( 'init', 'test_localtest_template' );
 }}}


 For me the result were -


 Current theme :**twentytwentytwo**
 path of current style.css asper locate_template : **/var/www/src/wp-
 content/themes/twentytwentytwo/style.css**
 Switching to :**twentytwentythree**
 Theme after switching **twentytwentythree**
 path of new style.css asper locate_template : **/var/www/src/wp-
 content/themes/twentytwentytwo/style.css**


 The issue was fist discovered while writing testcases for
 https://core.trac.wordpress.org/ticket/58576

 This was further discussed in detail on Making WordPress slack's **#core-
 performance** open channel- link to discussion
 (https://wordpress.slack.com/archives/C02KGN5K076/p1689770957730469)


 Similar issue may exist for `comments_template` and `is_child_theme`.

 Solution is to avoid use of `STYLESHEETPATH` and `TEMPLATEPATH` and
 instead use `get_stylesheet_directory()` and `get_template_directory()` at
 all places.


 Meanwhile we must also consider deprecating both the constants as it can't
 be updated by filter once set giving us two different values (one via
 constant and one via function) for the same thing.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58866>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list