Title: load_template
Published: April 25, 2014
Last modified: August 20, 2026

---

# load_template( string $_template_file, bool $load_once = true, array $args = array() )

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#wp--skip-link--target)

Requires the template file with WordPress environment.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#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](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#parameters)󠁿

 `$_template_file`stringrequired

Path to template file.

`$load_once`booloptional

Whether to require_once or require.

Default:`true`

`$args`arrayoptional

Additional arguments passed to the template.

Default:`array()`

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#more-information)󠁿

Uses global: (object) [$wp_query](https://developer.wordpress.org/reference/classes/wp_query/)
to extract [extract()](http://us.php.net/manual/en/function.extract.php) global 
variables returned by the query_vars method while protecting the current values 
in these global variables:

 * (unknown type) $posts
 * (unknown type) $post
 * (boolean) $wp_did_header Returns true if the WordPress header was already loaded.
   See the /wp-blog-header.php file for details.
 * (boolean) $wp_did_template_redirect
 * (object) $wp_rewrite
 * (object) [$wpdb](https://codex.wordpress.org/Class_Reference/wpdb)
 * (string) $wp_version holds the installed WordPress version number.
 * (string) $wp
 * (string) $id
 * (string) $comment
 * (string) $user_ID

## 󠀁[Source](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#source)󠁿

    ```php
    function load_template( $_template_file, $load_once = true, $args = array() ) {
    	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 ) ) {
    		/*
    		 * This use of extract() cannot be removed. There are many possible ways that
    		 * templates could depend on variables that it creates existing, and no way to
    		 * detect and deprecate it.
    		 *
    		 * Passing the EXTR_SKIP flag is the safest option, ensuring globals and
    		 * function variables cannot be overwritten.
    		 */
    		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
    		extract( $wp_query->query_vars, EXTR_SKIP );
    	}

    	if ( isset( $s ) ) {
    		$s = esc_attr( $s ); // @phpstan-ignore variable.undefined (It's extracted from query vars.)
    	}

    	/**
    	 * Fires before a template file is loaded.
    	 *
    	 * @since 6.1.0
    	 *
    	 * @param string $_template_file The full path to the template file.
    	 * @param bool   $load_once      Whether to require_once or require.
    	 * @param array  $args           Additional arguments passed to the template.
    	 */
    	do_action( 'wp_before_load_template', $_template_file, $load_once, $args );

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

    	/**
    	 * Fires after a template file is loaded.
    	 *
    	 * @since 6.1.0
    	 *
    	 * @param string $_template_file The full path to the template file.
    	 * @param bool   $load_once      Whether to require_once or require.
    	 * @param array  $args           Additional arguments passed to the template.
    	 */
    	do_action( 'wp_after_load_template', $_template_file, $load_once, $args );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/template.php#L782)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/template.php#L782-L829)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#hooks)󠁿

 [do_action( ‘wp_after_load_template’, string $_template_file, bool $load_once, array $args )](https://developer.wordpress.org/reference/hooks/wp_after_load_template/)

Fires after a template file is loaded.

 [do_action( ‘wp_before_load_template’, string $_template_file, bool $load_once, array $args )](https://developer.wordpress.org/reference/hooks/wp_before_load_template/)

Fires before a template file is loaded.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  |

| Used by | Description | 
| [do_feed_rdf()](https://developer.wordpress.org/reference/functions/do_feed_rdf/)`wp-includes/functions.php` |

Loads the RDF RSS 0.91 Feed template.

  | 
| [do_feed_rss()](https://developer.wordpress.org/reference/functions/do_feed_rss/)`wp-includes/functions.php` |

Loads the RSS 1.0 Feed Template.

  | 
| [do_feed_rss2()](https://developer.wordpress.org/reference/functions/do_feed_rss2/)`wp-includes/functions.php` |

Loads either the RSS2 comment feed or the RSS2 posts feed.

  | 
| [do_feed_atom()](https://developer.wordpress.org/reference/functions/do_feed_atom/)`wp-includes/functions.php` |

Loads either Atom comment feed or Atom posts feed.

  | 
| [locate_template()](https://developer.wordpress.org/reference/functions/locate_template/)`wp-includes/template.php` |

Retrieves the name of the highest priority template file that exists.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.5.0](https://developer.wordpress.org/reference/since/5.5.0/) | The `$args` parameter was added. | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#comment-content-727)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/load_template/#comment-727)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-727)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-727)
 4.  **Loading a template in a plugin, but allowing theme and child theme to override
     template**
 5.      ```php
         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' );
         }
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%3Freplytocom%3D727%23feedback-editor-727)
 7.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#comment-content-2971)
 8.    [webbuilder03](https://profiles.wordpress.org/webbuilder03/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/load_template/#comment-2971)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-2971)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-2971)
 10. **Send variable with [load_template()](https://developer.wordpress.org/reference/functions/load_template/)**
 11. you can send additional variable with [load_template()](https://developer.wordpress.org/reference/functions/load_template/).
     [load_template()](https://developer.wordpress.org/reference/functions/load_template/)
     extracts all of the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
     query variables, into the scope of the loaded template.
 12. use [set_query_var()](https://developer.wordpress.org/reference/functions/set_query_var/)
     to make your variable available to the template part.
 13.     ```php
         $template = locate_template('template.php');
     
         if( $template ){
     
         	set_query_var('my_variable ', 'Hello Template File' );
     
         	load_template( $template );
     
         }
         ```
     
 14. In **template.php** file you can access this variable like this
 15.     ```php
             //Output will be "Hello Template File"
         	echo $my_variable;
         ```
     
 16.  * Note that in recent WordPress `load_template` has a 3rd parameter for passing
        arguments to the template.
      * [strarsis](https://profiles.wordpress.org/strarsis/) [4 years ago](https://developer.wordpress.org/reference/functions/load_template/#comment-6407)
 17.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%3Freplytocom%3D2971%23feedback-editor-2971)
 18.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/load_template/?output_format=md#comment-content-6408)
 19.   [strarsis](https://profiles.wordpress.org/strarsis/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/load_template/#comment-6408)
 20. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-6408)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%23comment-6408)
 21. Note that the arguments set in `$args` are not simply made available as variables
     of the same name (as the key) in the template. Instead they are available in the`
     $args` variable in the template.
 22.  * I think it would be much better if any supplied arguments would be extracted
        so you could add hints for the variables in the template e.g.
      *     ```php
            public function renderShortcode(array $args): string {   $result = $this->loadNews();   $template = locate_template('news.php');   if (!$template) {     $template = __DIR__ . '/templates/news.php';   }   ob_start();   load_template($template, true, [     'result' => $result,   ]);   return ob_end_clean(); }
            ```
        
      *  The above will not work since we have the $result in the $args variable. I
        remove this entry since I do not understand how I can make a line break in 
        the code…
      * [cyclonecode](https://profiles.wordpress.org/cyclonecode/) [2 years ago](https://developer.wordpress.org/reference/functions/load_template/#comment-7112)
 23.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F%3Freplytocom%3D6408%23feedback-editor-6408)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fload_template%2F)
before being able to contribute a note or feedback.