• I’m a developer but new to developing themes for WordPress (though I used it before just as a CMS). I’ve trying searching for an explanation to this on Google and the WordPress forums but haven’t been able to find an answer. It seems that most WordPress themes open the html tag, define the doctype, and open the body in the header.php file and then close these tags in the footer.php. That seems strange and counter intuitive to me compared to having these tags in the index.php file. It’s even more confusing because the first div within the body is in the index.php file. The body tag and its immediate content become separated from one another.

    It makes more sense to me to have the index.php both open and close the html and body tags. You would then include the calls for the head, header, and footer within this skeleton.

    So my question: is there a reason that the WordPress was designed this way to have the html and body tags opened and closed in two separate files, neither of which are index.php? Is there some benefit or advantage to doing it this way rather than having it all in the index file? I cannot see a reason, and it seems to me that it makes everything more complicated.

    Much appreciated in advanced.

Viewing 7 replies - 1 through 7 (of 7 total)
  • is there a reason that the WordPress was designed this way to have the html and body closed in two separate files

    Have you reviewed Theme Development and the theme template hierarchy? Have you taken into account template files such as single.php, content.php, category.php etc etc?

    Thread Starter colinacronin

    (@colinacronin)

    Yes I have. Do the existence of those other files make a difference? Wouldn’t you just construct each of them in the same manner? In most cases I would assume you would just modify the content between the body tags for such pages as the post/page, category, etc. And if you wanted to do something more with the header or footer you would either modify that for those pages, or have some kind of conditional PHP that changes those sections automatically.

    Thread Starter colinacronin

    (@colinacronin)

    Perhaps this will help illustrate. Here are two examples of a WordPress index.php file. The first is how it would look by default

    <?php
    /**
    * The main template file
    *
    * This is the most generic template file in a WordPress theme
    * and one of the two required files for a theme (the other being style.css).
    * It is used to display a page when nothing more specific matches a query.
    * For example, it puts together the home page when no home.php file exists.
    *
    * @link https://codex.www.ads-software.com/Template_Hierarchy
    *
    * @package WordPress
    * @subpackage Twenty_Twelve
    * @since Twenty Twelve 1.0
    */
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    This second version is how I would write it.

    <?php
    /**
    * The main template file
    *
    * This is the most generic template file in a WordPress theme and one
    * of the two required files for a theme (the other being style.css).
    * It is used to display a page when nothing more specific matches a query,
    * e.g., it puts together the home page when no home.php file exists.
    *
    * @link https://codex.www.ads-software.com/Template_Hierarchy
    *
    * @package WordPress_Themes
    * @subpackage Custom_Theme
    */
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    
    	<?php get_template_part( 'head', 'index' ); /* Includes <head> info, wp_head(), etc */ ?>
    
    	<body <?php body_class(); ?>>
    
    		<?php get_header(); ?>
    
    		<div id="primary" class="site-content">
    
    		</div>
    
    		<?php get_footer(); ?>
    
    	</body>
    
    </html>

    This seems to make more sense to me, because you can see how the entire webpage is put together. You have the opening html tag, a call for the head section, the opening body tag, the body content, a call for the footer, the closing body tag, and the closing html tag. All of this is in one file so it is easy to understand.

    Is there some function of WordPress that my way misses? I have tried it on a test site and everything seems to be running properly. But this is different than the official WordPress documentation, so I want to make sure I’m not missing something crucial.

    Do the existence of those other files make a difference?

    Yes. They can be called via the template hierarchy at multiple points in a site.

    Wouldn’t you just construct each of them in the same manner?

    Why replicate so much code? Why have to edit a footer in 12 different files using your approach when you only have to edit 1 file using the standard WP method?

    And then there’s child themes. Using your approach & the same footer example, a child theme could need far more custom template files and may no longer correctly reflect updates in the parent theme.

    Thread Starter colinacronin

    (@colinacronin)

    You wouldn’t need to edit multiple footers. You would call the footer the same way in each of those 12 files as you mention. If you want to edit the footer you just edit the footer.php file.

    And how would this work with single.php?

    I’m stumbling across this thread months after it went stale. But I completely agree, colinacronin! Its crazy to me that one file would include an opening tag and another file would include a closing tag.

    For readability, reusability, and consistency with other (non-Wordpress) development styles, source files should be as self-contained as possible. Putting the html tags in index.php would result in no extra code but would produce the benefit of showing the context over which that file operates. It also assists with self-documents of the files’s purpose.

    But I have noticed the first few themes I have inspected all follow this strange rule of spreading tags across files. Did anyone ever find a reason explaining why WordPress developers have chosen to break tag opening/closing across multiple files?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Why not open and close html/body in index.php file’ is closed to new replies.