2

I've been coding a Wordpress theme using OOP PHP, node.js and Webpack, and between classes, node_modules, configuration files and Wordpress template files my theme's directory has become a bit messy...

Is there an easy way to move the theme's template files to an app subfolder without breaking the theme?

(I've found a similar question but it was asked in 2011, so hopefully something came up over time).

1
  • you mean you want to move the files and the theme will still continue to work ? Commented Oct 11, 2017 at 7:47

1 Answer 1

2

Out of the box WP relies on Template Hierarchy to resolve and load template. The default assumption is that (most) template are in the root of the theme and follow the naming conventions.

Placing template files elsewhere essentially requires rebuilding Template Hierarchy in your code with different assumptions. This used to be crazy inconvenient, but WP 4.7 introduced {$type}_template_hierarchy hook, which makes it significantly easier.

As a personal aside I think native WP template simply doesn't scale meaningfully to complex use cases. If there are enough templates to clutter the directory, then I would move to a different template engine (such as Twig) altogether. Of course that is hardly mainstream technique in WP development.

I have an example of template hierarchy override in my Meadow project for Twig integration, but same works for just changing up logic for PHP templates.

2
  • Hi, thank you so much for your answer! I googled the {$type}_template_hierarchy hook and found this code sample [link] (reddit.com/r/Wordpress/comments/6yh1rv/…) on Reddit (bottom of the page) made specifically for Wordpress and which is working for me, in part. What's not working are the custom functions that replace get_header, get_sidebar and get_footer. I noticed header, sidebar and footer are also missing from your Twig class, so I was wondering if you could give any help with those as well? I would really appreciate it!
    – grazdev
    Commented Oct 11, 2017 at 8:53
  • Sorry, my mistake! The whole code sample is actually working for me, so I guess I'm done. And thank you for putting me on the right way.
    – grazdev
    Commented Oct 11, 2017 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.