apply_filters( ‘template_include’, string $template )

Filters the path of the current template before including it.

Parameters

$templatestring
The path of the template to include.

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.

Source

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

Changelog

VersionDescription
3.0.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    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 4 content
    /**
    * 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;			
    	}
    
    	return $template;
    }

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