I can create my theme without splitting it into separate files. Why do theme developers split WordPress theme into a few single files like header.php sidebar.php and etc?
You need that for child themes. If you have a separate
header.php a child theme can use its own
header.php and override the parent theme’s file. Plus,
footer.php are used in
Another reason is readability: Putting all the code for a theme into just one file gets very messy very fast.
A use case for a compact theme is testing and debugging: My Mini Theme uses just
comments.php for output. Whenever a plugin doesn’t do what I expected I use that theme to be sure it is not a theme issue.
Regardless of the platform (WordPress, Joomla!, Drupal) or the language (PHP, C++, VB) it's pretty much accepted/standard practice to break out code in to functional units. This includes not only functions and classes, but files as well.
As @Toscho points out:
- The code is used in multiple places.
- It improves readability.
It also makes it easier to:
- Maintain your code.
- Reuse it in other areas.
- Organize it in to manageable chunks/blocks.
- Work in a team environment.
The list could go on for a very long time.
Both @toscho's and @stephen's answers good answers, and I upvoted them, but also remember that WordPress looks for particular template files depending upon context. For example, a file named
category.php will be used automatically for category archives.
author.php will be used for author archives. If you choose not to use the built in template architecture you will have to build those switches (or at least the ones you want) into your code, which would be tedious and redundant.