Make WordPress Core

Opened 20 months ago

Closed 19 months ago

Last modified 19 months ago

#24814 closed enhancement (wontfix)

Allow placing theme templates in subdirectory

Reported by: bungeshea Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Themes Keywords:
Focuses: Cc:


It would be nice if all theme templates, such as footer.php, header.php, and content parts such as content.php could be placed in a subdirectory in the theme, such as templates/, instead of in the theme root.

For a standard theme with few templates, placing the templates in the root is okay. However, when you add in content parts for post formats, and separate out the loop, you can end up with quite a few templates.
Add a screenshot.png, style.css, minified style.css, editor-style.css, readme, license, and perhaps a GruntJS file, Git config files, or a Compass config file, and the theme root becomes quite cluttered. Allowing templates files to be separated in a subdirectory would result in a much cleaner directory structure.

Change History (15)

comment:1 @bungeshea20 months ago

The locate_template() function would need to be modified to accomidate this change. I can't think of what else, though.

comment:2 @anand.potukuchi20 months ago

Type changed to enhancement

comment:3 @anand.potukuchi20 months ago

  • Keywords reporter-feedback dev-feedback added

comment:4 @johnbillion20 months ago

FYI you can place content parts in subdirectories. Just use get_template_part( 'my-dir/my-file' ).

comment:5 @alexvorn220 months ago

header.php and footer.php should be as it is in the root folder.
For additional .css files I think they can fit well in a separate folder css/, and .js files in a js folder.

comment:6 @colorful tones20 months ago

I prefer to use get_template_part for this type of abstraction: https://codex.wordpress.org/Function_Reference/get_template_part as well.

Also, the change suggested would entail a lot of user (WP community) training. The theme template file structure has been incorporated into a lot of training, which has enabled the ease-of-use for varying levels of users. i.e. it might be beneficial for theme developers, but not necessarily for somebody new, or inexperienced that might be trying to figure out where to update the copyright info in a footer.php file for a theme they downloaded. Hope that makes sense :-)

comment:7 @retlehs20 months ago

  • Cc retlehs added

comment:8 @SergeyBiryukov20 months ago

  • Component changed from General to Themes
  • Keywords 2nd-opinion close added; reporter-feedback dev-feedback removed

As noted in comment:4, placing content parts in subdirectories is already possible.

Same goes for page templates, see #11216.

Same goes for additional CSS and JavaScript files, see Twenty Thirteen for example.

The ability to place standard templates in a subdirectory as well sounds like unnecessary complication of the template hierarchy to me.

comment:9 @bungeshea20 months ago

I am aware that template parts may be stored in a subdirectory, however it is convention to store them with regular template parts. Additional CSS and JavaScript can be stored in subdirectories, but style.css cannot, and it is convention to store editor-style.css also in the theme root; plus style.min.css to work with the SCRIPT_DEBUG convention.

For proof of how cluttered a theme root can get, see https://github.com/bungeshea/theme-boilerplate. Additional CSS and all JavaScript, as well as fonts, extra functions includes, images, language files are stored in separate directories. It would be irresponsible to move content-*.php, loop-*.php or menu-*.php templates to a subdirectory, and all of the other templates are required to be in the root. As well as the templates and the three stylesheets, I also have a package.json and Gruntfile.js for Grunt, a config.rb for Compass, .gitignore and .gitmodules for Git, .jshintrc for JSHint, a license and a readme.

Call me OCD, but I find it difficult to make my way around the theme while coding, and prefer my files to be nicely organized. I am not asking for it to be required for templates to be in a subdirectory - only to make it possible.

comment:10 @nacin20 months ago

You do have one option: To avoid the hierarchy entirely and write your own handling that includes subdirectories. (That's basically what you'd be doing anyway.)

I agree SergeyBiryukov. Let's not complicate the template hierarchy, please.

It's not just about personal levels of OCD — it is also about how easy it is for a WordPress user or beginning developer to understand a theme when they first open it up. Despite all of the variations in theme development, the theme hierarchy is a strong, important, and recognizable foundation.

comment:11 @alexvorn220 months ago

  • Keywords 2nd-opinion removed

comment:12 follow-up: @bungeshea20 months ago

@nacin so basically you mean fork the locate_template() function, and copy the functions that rely on it for my personal use? That seems like an overcomplication. All that needs to be added to core is the the ability to filter the $located variable in get_template_part().

comment:13 in reply to: ↑ 12 @SergeyBiryukov20 months ago

Replying to bungeshea:

All that needs to be added to core is the the ability to filter the $located variable in get_template_part().

Related: #12877, #13239, #14310, #21062.

comment:14 @c3mdigital19 months ago

  • Keywords close removed
  • Resolution set to wontfix
  • Status changed from new to closed

Current theme template hierarchy should not be complicated.

comment:15 @SergeyBiryukov19 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.