Auto get_header and get_footer on every template?

Is there a way to automatically run get_header() at the beginning of template and get_footer() at the end of it?

Currently my code is too repetitive with those calls.

I’ve been looking for a way to do this, but I can’t find it in Google.

2 s
2

Looking at wp-includes/template-loader.php … there seems to be a way:

if ( $template = apply_filters( 'template_include', $template ) )
    include( $template );

You could hook into that filter, handle the including in a callback function and return FALSE.

Sample code, not tested:

add_filter( 'template_include', function( $template ) {

    get_header();
    include $template;
    get_footer();

    return FALSE;
});

Leave a Comment