[wp-trac] [WordPress Trac] #31475: Add ability to change the folder location for templates

WordPress Trac noreply at wordpress.org
Fri Feb 27 16:03:53 UTC 2015

#31475: Add ability to change the folder location for templates
 Reporter:  rugbert      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Themes       |    Version:  4.1
 Severity:  normal       |   Keywords:
  Focuses:               |
 I've seen questions here and there on stack exchange asking how to change
 the location WP looks for templates and I have wanted to do this for a
 long time,  and as it stands now, it is not possible to change the
 location of the WP templates without changing wp core. But I've gone ahead
 and worked something out.

 First tho, why should this be change? I see 3 good reasons.

 1. It allows developers set up a folder structure which more closely
 resembles a MVC framework which is, by definition, more organized and
 cleaner than what wordpress expects.
 2. It complements the Timber plugin
 3. Because it's easy to impliment and can be 100% optional for developers

 Here's how I did it:
 In your theme's function.php (or in wp-config.php) we create a constant
 called something like "'''WP_TEMPLATE_DIR'''" and setting the value to the
 name of the desired directory.

 define('WP_TEMPLATE_DIR', 'controllers');

 Then  the '''location_template''' function needs to be modified, see below

 Note - I cant highlight the changes due to code block formatting, but
 essentially we:
 1. Set a variable called $path to an empty string
 2. Check to see if the constant is defined and the specified folder
 exists. If it does we set the $path variable to the specified folder
 3. we simply inject the $path variable into the  '''TEMPLATEPATH . '/' .
 $template_name;''' So if the constant is not set, path will just be an
 empty string and therefore the path location will not change.

 function locate_template($template_names, $load = false, $require_once =
 true ) {
         $located = '';
         foreach ( (array) $template_names as $template_name ) {
                 if ( !$template_name )

         $path = '';
         if( defined('WP_TEMPLATE_DIRECTORY')
             && ( !empty(WP_TEMPLATE_DIRECTORY) &&
             && file_exists( STYLESHEETPATH . '/' . WP_TEMPLATE_DIRECTORY)

             $path = WP_TEMPLATE_DIRECTORY. '/';

         if ( file_exists(STYLESHEETPATH . '/' . $path . $template_name)) {
                         $located = STYLESHEETPATH . '/' . $path .
                 } else if ( file_exists(TEMPLATEPATH . '/' . $path .
 $template_name) ) {
                         $located = TEMPLATEPATH . '/' . $path .

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

         return $located;

 So wordpress is now looking for templates in the "controllers" folder that
 is located in the root of my theme. Now when I set up timber, I can have a
 folder called "views" for the twig tempalates, next to a folder called
 "controllers" for my actual wordpress templates and then I could set up my
 custom post types/taxonomies/whatever in a folder called models.

 Now my theme folder only has functions.php, style.css, and then some
 folders to organize everything.

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

More information about the wp-trac mailing list