Why get_header() or get_footer() does not run twice if called in the same php file?

The actual templates are loaded with require_once, so PHP automatically ignores the second attempt to load them. (You will trigger the ‘get_header’ hook twice though.)

Here’s the relevant code in get_header():

    $templates[] = 'header.php';

    if ( ! locate_template( $templates, true, true, $args ) ) {
        return false;
    }

The third parameter to locate_header(), the second true, is the load-with-require-once flag. There’s no filter we can use to change that behaviour here.

Note that you could include two different header templates, e.g. if you created a copy of header.php as header-duplicate.php in your theme then

get_header();
get_header('duplicate');

would include them both, as this wouldn’t get blocked by require_once.

Leave a Comment