apply_filters( 'template_include', string $template )

Filters the path of the current template before including it.


Parameters Parameters

$template

(string) The path of the template to include.


Top ↑

More Information More Information

This filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress’s default template behavior.


Top ↑

Source Source

File: wp-includes/template-loader.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Julio Potier

    This example includes a new template on a page called ‘portfolio’ if the new template file was found.

    add_filter( 'template_include', 'portfolio_page_template', 99 );
    function portfolio_page_template( $template ) {
        if ( is_page( 'portfolio' )  ) {
            $new_template = locate_template( array( 'portfolio-page-template.php' ) );
    	if ( '' != $new_template ) {
    	    return $new_template ;
    	}
        }
        return $template;
    }
  2. Skip to note 2 content
    Contributed by Rezwan Shiblu
    /**
    * multiple custom routing with one page
    */
    add_filter( 'template_include', 'wpdocs_include_template_files_on_page' );
    
    function wpdocs_include_template_files_on_page( $template ) {
    
    	$action = isset( $_GET['action'] ) ? $_GET['action'] : 'list';
    
    	switch ( $action ) {
    
    		case 'add-list' :
    			$template = __DIR__ . 'views/address-new.php';
    			break;
    
    		case 'edit-list' :
    			$template = __DIR__ . 'views/address-edit.php';
    			break;
    
    		case 'view-list' :
    			$template = __DIR__ . 'views/address-view.php';
    			break;
    
    		default :
    			$template = __DIR__ . 'views/address-list.php';
    			break;			
    	}
    
    	if ( file_exists( $template ) ) {
    		include $template;
    	}
    
        return $template;
    }
    

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