Support » Fixing WordPress » Why are the theme files organized like this?

  • I would like to know, why the themes in WordPress use whole page HTML layout code divided into separate files (header.php and footer.php), instead of one layout file, that would inject the main content template inside itself. IMHO it would be more consistent if the whole page layout would be united in one file.

    • This topic was modified 1 year, 9 months ago by rzelnik.
    • This topic was modified 1 year, 9 months ago by rzelnik.
Viewing 6 replies - 1 through 6 (of 6 total)
  • @rzelnik

    A single file would work but it may well also result is a lot of redundant code being called fir each page load depending in the complexity of the theme.

    This page will hopefully explain far better than I can in a forum response:

    https://codex.wordpress.org/Stepping_Into_Templates

    Thread Starter rzelnik

    (@rzelnik)

    @mattyrob Thank your for your answer. What redundant code do you mean? Actually the current scheme creates redundancy. You need to load header, sidebar, footer in each template. With the layout > content scheme you only need to load them once – in the layout template. For example:

    core:

    
    $template = "index.php"; // or 404.php, archive.php, single.php etc.
    include "layout.php";
    

    layout.php

    
    <!doctype html>
    <html>
        <head>
            ...
        </head>
        <body>
            <?php
            get_header();
            include $template;
            get_sidebar();
            get_footer();
            ?>
        </body>
    </html>
    

    Most of the CMSes and web application frameworks use this scheme, and in my opinion it works better. You can use multiple different layout files when needed, of course.

    • This reply was modified 1 year, 9 months ago by rzelnik.

    @rzelnik

    Some pages that load in themes don’t need or load the sidebar doe example, so a single file that contains header, content, sidebar, footer, comments etc would load the sidebar code on pages that don’t render it.

    Thread Starter rzelnik

    (@rzelnik)

    Yes, I understand. In this case you can use conditional load or multiple layout templates. The point is that the page layout code is not splitted into two separate files, so it is easier to read.

    It is because of the “WordPress way” of hooks, that makes WordPress so flexible. Each template file that is loaded calls an action for that template, so plugins can affect the different parts easily (as well as child themes).
    It is much more flexible to build the page in pieces that are shared across pages than to have each page duplicating logic. The content area is not the only place where the theme changes something.

    Thread Starter rzelnik

    (@rzelnik)

    Ok @joyously , thanks for your answer.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Why are the theme files organized like this?’ is closed to new replies.