load_template( string $_template_file, bool $require_once = true )

Require the template file with WordPress environment.


Description Description

The globals are set up for the template file to ensure that the WordPress environment is available from within the function. The query variables are also available.


Parameters Parameters

$_template_file

(string) (Required) Path to template file.

$require_once

(bool) (Optional) Whether to require_once or require.

Default value: true


Top ↑

Source Source

File: wp-includes/template.php

function load_template( $_template_file, $require_once = true ) {
	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

	if ( is_array( $wp_query->query_vars ) ) {
		extract( $wp_query->query_vars, EXTR_SKIP );
	}

	if ( isset( $s ) ) {
		$s = esc_attr( $s );
	}

	if ( $require_once ) {
		require_once( $_template_file );
	} else {
		require( $_template_file );
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Loading a template in a plugin, but allowing theme and child theme to override template

    if ( $overridden_template = locate_template( 'some-template.php' ) ) {
    	/*
    	 * locate_template() returns path to file.
    	 * if either the child theme or the parent theme have overridden the template.
    	 */
    	load_template( $overridden_template );
    } else {
    	/*
    	 * If neither the child nor parent theme have overridden the template,
    	 * we load the template from the 'templates' sub-directory of the directory this file is in.
    	 */
    	load_template( dirname( __FILE__ ) . '/templates/some-template.php' );
    }
    

You must log in before being able to contribute a note or feedback.