WordPress Hierarchical Page Template Redirect Plugin

WordPress’ hierarchical page structure makes a wonderful platform for developing a Content Management System (CMS).  Pages can stand alone at the top of the WordPress hierarchy (along with any siblings at the same level) or they can contain an entire tree of descendant pages (child-pages), each with their own potential hierarchical structure.

Besides this tree-like page structure WordPress gives the web designer the flexibility of assigning pages their own templates so that each page can have its own distinct look and feel.   WordPress even gives the developer the flexibility of several ways of assigning templates to a page.

However, when it comes to the applying of a template to a page, WordPress has a strange anomaly.  A page must be intentionally assigned a template (or have a template file intentionally named for it) or WordPress applies the default page.php.  There is no inheritance when it comes to the application of templates to pages.  On a large site with many child pages within a hierarchy of pages this does not make for an easy or error free way to create the compartmentalization mentioned above.  A user can assign a template to a top level page (or a “beginning of section” page) but its descendants (child pages) will not follow with their ancestor’s template.  Each page’s template must be individually assigned.  For small sites with few pages and few users this is not a significant matter.  On a site with hundreds or thousands of pages and multiple editors and multiple “compartments” this makes WordPress less than ideal as a CMS platform – unless a remedy can be found.

So the challenge I have undertaken is to develop a plugin whereby this anomaly is remedied. My philosophy for this is that if a child page does not have a specified template then it acquires the template of its parent page and if its parent does not have a designated template then it acquires the template of the next ancestor up in the page hierarchy and so on until a template is found.

The corollary to this is that within a hierarchy of pages a theme can be disrupted (a new template interjected) at any time by the introduction of a page-specific template file at the level where the change needs to take place.  Once a new template is introduced into the hierarchy then all children of that page (with the new template) will acquire that template as well until another template is introduced.

Because both page-[id].php and page-[slug].php are called on the fly – there is no meta data attached to the page itself to tell WordPress that those templates exist.  Therefore a check must be made to determine if there is any template file in the context of the current page’s ancestry.

This plugin does that search and assigns hierarchical templates to pages and their children giving precedence to page-[id].php templates. If no page-[id].php templates are found all the way up the ancestry tree then it will check for page-[slug].php templates back up the ancestry tree. If those are not found then it will look for a meta assigned custom.php template – first for the currently selected page and then if the page does not have a custom.php template assigned the function will look back up the ancestor tree to see if an ancestor page has been assigned a custom.php template. If all of these fail it will fall back to WP’s default page.php template.

The Hierarchical Page Template Redirect Plugin is currently driving the page templates of this site: http://www.nbseminary.ca

The plugin can be downloaded below or from the WordPress plugin repository [HPTR].

  1. Download the file
  2. Extract and upload hierarchical-page-template-redirect.php to the /wp-content/plugins/ directory
  3. Activate the plugin through the ‘Plugins’ menu in WordPress

Leave a Reply