Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 5 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 (16)

#1 @bungeshea
2 years ago

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

#2 @anand.potukuchi
2 years ago

Type changed to enhancement

#3 @anand.potukuchi
2 years ago

  • Keywords reporter-feedback dev-feedback added

#4 @johnbillion
2 years ago

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

#5 @alexvorn2
2 years 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.

#6 @colorful tones
2 years 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 :-)

#7 @retlehs
2 years ago

  • Cc retlehs added

#8 @SergeyBiryukov
2 years 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.

#9 @bungeshea
2 years 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.

#10 @nacin
2 years 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.

#11 @alexvorn2
2 years ago

  • Keywords 2nd-opinion removed

#12 follow-up: @bungeshea
2 years 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().

#13 in reply to: ↑ 12 @SergeyBiryukov
2 years 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.

#14 @c3mdigital
2 years ago

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

Current theme template hierarchy should not be complicated.

#15 @SergeyBiryukov
2 years ago

  • Milestone Awaiting Review deleted

#16 @dd32
5 months ago

#32854 was marked as a duplicate.

Note: See TracTickets for help on using tickets.