[wp-trac] [WordPress Trac] #51691: Call get_template_directory() once instead of multiple times for loading theme's files (performance boost)

WordPress Trac noreply at wordpress.org
Sun Nov 1 18:47:19 UTC 2020


#51691: Call get_template_directory() once instead of multiple times for loading
theme's files (performance boost)
----------------------------+-----------------------------
 Reporter:  hellofromTonya  |      Owner:  (none)
     Type:  task (blessed)  |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Bundled Theme   |    Version:  trunk
 Severity:  normal          |   Keywords:
  Focuses:  performance     |
----------------------------+-----------------------------
 In the bundled themes, `get_template_directory()` is invoked for loading
 each of the theme's files, i.e. in the theme's `functions.php` file.

 This degrades the performance of the theme's initialization. How so?
 ''Each time'' `get_template_directory()` runs, ''multiple'' other
 functions and filters also run including these 5 hooks:

 - `pre_option_template`
 - `alloptions`
 - `option_template`
 - `theme_root`
 - `template_directory`

 The number of times `get_template_directory()` is called for loading theme
 files:

 - Twenty Twenty One: 12 times
 - Twenty Twenty: 12 times
 - Twenty Nineteen: 10 times
 - etc.

 This ticket proposes a performance gain for the bundled themes by:

 - replacing the `get_template_directory()` with a private memoized theme
 function
 - calling `get_template_directory()` only once and then storing it into a
 `static` variable for reuse within the private function


 This change reduces memory, CPU cycles, and processing time.

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


More information about the wp-trac mailing list