I have to confess I’m struggling to see how the WordPress Twenty Fourteen theme picks up templates put in the theme’s page-templates
folder. Previously I’d create new templates named page-<slug>.php
in the theme’s root folder, but not now.
I’ve searched the code for mentions of page-templates
and not found any where files are included; searched the WP source code too and found none.
Can you explain how they’re detected and picked up please?
The actual query for a Theme’s page templates happens in wp_get_theme()->get_page_templates()
(source), which in turn calls get_files()
(source).
The magic happens here:
$files = (array) $this->get_files( 'php', 1 );
The args for get_files()
are:
function get_files( $type = null, $depth = 0, $search_parent = false ) {}
So, when WordPress searches the Theme for PHP files, it passes a $depth
argument of 1
, which means that it searches in subdirectories of the Theme root directory.