Automatically Link WordPress Slugs to Custom Page Templates

Konstantin Kovshenin has come up with a neat way to link slugs to Custom Page Templates without manually assigning a template to a page. He’s calling the plugin Static-Templates.

Static-Templates comes in handy when you have a highly customized WordPress site. Ordinarily each Custom Page that needs a slightly different layout needs to be assigned a Custom Template in the WordPress Admin. Using the Template Dropdown to do this when there are a large number of pages is quite cumbersome and error-prone.

Instead what Static-Templates does is it hooks into WordPress’s template_include filter. The filter matches the URL slug, like say art-gallery against a template located at your theme’s static-templates/art-gallery.php file. If the template file is found it’s path gets passed to WordPress’s template loader.

Another cool thing about this technique is that it also ends up saving a Database query! While WordPress needs to access the database to pick up the name of Custom Template, Static-Templates does this merely by convention!

Konstantin built this for a custom project for a friend, but I think it is quite appealing as a general theme customization convention.

Nice work Konstantin. Make it official now, before someone else does!

  • Peter

    This has been a builtin feature in WordPress forever.

    • Darshan Sawardekar

      Hi Peter,

      This is very similar. However with Pages you still need to create the Pages in the WordPress Admin.

      Imagine creating a bunch of Pages alongside their templates with page-foo.php. Doing so locally works fine but to push to production, you have to recreate these Pages once more. In Konstantin’s case he would have had to create ~40 Pages manually.

      That’s what this plugin allows, you only need to create /static-templates/foo.php.

      • nvartolomei

        What is WordPress export/import functionality for?