Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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, header.php and footer.php are used in wp-signup.php.

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 index.php and 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.

share|improve this answer
    
Thanks @toscho, you solved my problem :) –  Farhad Shirzad Nov 23 '12 at 19:20

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:

  1. The code is used in multiple places.
  2. It improves readability.

It also makes it easier to:

  1. Maintain your code.
  2. Reuse it in other areas.
  3. Organize it in to manageable chunks/blocks.
  4. Work in a team environment.
  5. ...

The list could go on for a very long time.

share|improve this answer

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.

share|improve this answer
    
You don’t need any of those switches depending on the styles you are using. –  toscho Nov 23 '12 at 22:10
    
It is true that you might not need any of the template hierarchy functionality. That is why I said, "or at least the ones you want". –  s_ha_dum Nov 23 '12 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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