[wp-trac] [WordPress Trac] #40731: locate_template() preformance improvment

WordPress Trac noreply at wordpress.org
Thu May 11 12:11:53 UTC 2017


#40731: locate_template() preformance improvment
-----------------------------------+-----------------------------
 Reporter:  danielhuesken          |      Owner:
     Type:  enhancement            |     Status:  new
 Priority:  normal                 |  Milestone:  Awaiting Review
Component:  Themes                 |    Version:  4.6.4
 Severity:  normal                 |   Keywords:
  Focuses:  template, performance  |
-----------------------------------+-----------------------------
 If the theme uses massive the *_template functions it can come to a slow
 performance because the the files will be searched again and again.

 We can improve the performance if we use a static cache like this:

 {{{
 function locate_template($template_names, $load = false, $require_once =
 true ) {
         static $template_cache;

         $located = '';
         foreach ( (array) $template_names as $template_name ) {
                 if ( !$template_name )
                         continue;
                 if ( $template_cache[$template_name] ) {
                         $located = $template_cache[$template_name];
                         break;
                 }
                 if ( isset( $template_cache[$template_name] ) && !
 $template_cache[$template_name] )
                         continue;
                 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
                         $located = STYLESHEETPATH . '/' . $template_name;
                         $template_cache[$template_name] = $located;
                         break;
                 } elseif ( file_exists(TEMPLATEPATH . '/' .
 $template_name) ) {
                         $located = TEMPLATEPATH . '/' . $template_name;
                         $template_cache[$template_name] = $located;
                         break;
                 } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/'
 . $template_name ) ) {
                         $located = ABSPATH . WPINC . '/theme-compat/' .
 $template_name;
                         $template_cache[$template_name] = $located;
                         break;
                 }
                 $template_cache[$template_name] = false;
         }

         if ( $load && '' != $located )
                 load_template( $located, $require_once );

         return $located;
 }
 }}}

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


More information about the wp-trac mailing list