[wp-trac] [WordPress Trac] #41362: Make locate_template() filterable to change the template locations
WordPress Trac
noreply at wordpress.org
Wed Jul 19 02:07:05 UTC 2017
#41362: Make locate_template() filterable to change the template locations
----------------------------+-----------------------------
Reporter: kylejennings83 | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version: 4.8
Severity: normal | Keywords:
Focuses: template |
----------------------------+-----------------------------
Right now WordPress expects all templates to reside in the root of your
theme directory. We have all come across a theme with dozens of templates
and well, that can get messy. With 6 lines of code we can easily, and
vastly enhance the templating system.
This would provide a number of benefits:
1) This would allow developers to logical group functionally similar files
/ separates functional dissimilar files
2) It allows the root of the theme directory to be cleaned up and used for
"resources" such as the functions.php and style.css files
3) By having the ability to pick and choose which templates go where, we
can turn WordPress into an MVC application framework (personally what I
use this technique for)
4) Related - this plays really nice with Timber
5) This lays the groundwork for future enhancements which I also use and
plan to introduce shortly
= Examples =
== Moving all templates with the {{{"templates_path"}}} filter ==
Say I wanted to move all my templates into a directory named "templates"
because as mentioned, this theme I inherited has dozens of templates,
maybe one for each page. A good start would be to just move all the
templates into a directory named "templates":
{{{#!php
<?php
function move_templates($path){
return 'templates';
}
add_filter('templates_path', 'move_templates');
}}}
This filter would move ALL templates to the "templates" directory (and has
the added effect of also moving the location of the header, footer ect ect
files as well) . So now the root of our theme is nice and clean:
{{{
/twentyseventeen/
|-- /assets/
|-- /inc/
|-- /templates/
|-- functions.php
|-- index.php
|-- README.txt
|-- rtl.css
|-- screenshot.png
|-- style.css
}}}
Now obviously just moving dozens of templates into a different directory
just moves the mess out of site, but that directory is still a mess and
just a stew of unrelated templates.
== Group related templates together with the
{{{"{$type}_templates_path"}}} filter ==
To clean things up even more. Imagine if 10 of these templates were all
children of the page "team". You could group them all together into a
subdirectory of "templates" called "team" like so:
{{{#!php
<?php
function move_team_pages($path = '', $type = null){
$parent = get_page_by_title( 'team' );
$parent_id = $parent->ID;
$obj = get_queried_object();
if($obj->post_parent == $parent_id)
$path = rtrim($path, '/') . '/' . 'team';
return $path;
}
add_filter('page_templates_path', 'move_team_pages');
}}}
Now your theme might look something like:
{{{
/twentyseventeen/
|-- /assets/
|-- /inc/
|-- /templates/
|-- index.php
|-- single.php
|-- /team/
|-- page-mortimer.php
|-- page-biff.php
}}}
As we see, the two filters can (and perhaps should) be used together, but
can also be used alone.
== Timber ==
As mentioned before, I use something like this on every project. I
generally put all my templates into a directory called "controllers" and
my twig files (if using Timber) into a directory called "views" so I have
something akin to an MVC application.
My functions file sets Timber to look for twig files in a directory called
"views" which is right inside the theme directory (next to "templates"):
{{{
$timber = new \Timber\Timber();
Timber::$dirname = array('views');
}}}
so my {{{ templates/index.php}}} could set up my Timber context and pass
it all the way to the {{{ index.twig }}} file in the "views" directory:
{{{
$context = Timber::get_context();
$context['posts'] = Timber::get_posts();
Timber::render('index.twig', $context);
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41362>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list