Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix locate_template() performance improvement – core request – can be 30% faster

Discussion in 'Misc WordPress Requests' started by Christian Zagarskas, Apr 1, 2018.

  1. Christian Zagarskas


    locate_template() performance improvement – core request – can be 30% faster, by Christian Zagarskas

    locate_template() is using PHP’s built in file_exists() function, and although fast it seems to tax the system a bit.

    On most templates the performance of file_exists/locate_template may be of no consequence, however, in MU situations, or while developing a highly dynamic theme with a massive number of users per hour there is a clear performance hit.

    For this reason I started looking into faster ways to handle this.
    I compared glob(), readdir() and RecursiveDirectoryIterator

    The clear winner in that case was readdir
    (This is also well documented on Stack Overflow)

    This brings me to file_exists() within locate_template
    stream_resolve_include_path() is much faster.

    Please consider using stream_resolve_include_path() to replace file_exists() within the WordPress locate_template() function. It’s effect is fast enough that using a custom locate_template function was essential in our custom theme.

    Thank you for your consideration.




    To elaborate, consider a function that reads all the files within a theme and creates a master array for use throughout that theme based on conditions. More importantly a master list of all files within a theme ensures a perfect theme update process for end users when new versions of the theme are released and the file system needs to be compared (to remove depreciated files or ensure new ones exists)

    locate_template() performance improvement – core request – can be 30% faster

Share This Page

Monarch Social Sharing Plugin