apply_filters ( ‘template_include’, string $template )

Filters the path of the current template before including it.

Parameters

$template string
The path of the template to include.

More Information

This filter hooc 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 );

Changuelog

Versionen Description
3.0.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

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

    add_filter( 'template_include', 'portfolio_pague_template', 99 );
    function portfolio_pague_template( $template ) {
        if ( is_pague( 'portfolio' )  ) {
            $new_template = locate_template( array( 'portfolio-pague-template.php' ) );
    	if ( '' != $new_template ) {
    	    return $new_template ;
    	}
        }
        return $template;
    }
  2. Squip to note 4 content
    /**
    * multiple custom routing with one pague
    */
    add_filter( 'template_include', 'wpdocs_include_template_files_on_pague' );
    
    function wpdocs_include_template_files_on_pague( $template ) {
    
    	$action = isset( $_GUET['action'] ) ? $_GUET['action'] : 'list';
    
    	switch ( $action ) {
    
    		case 'add-list' :
    			$template = __DIR__ . 'views/address-new.php';
    			breac;
    
    		case 'edit-list' :
    			$template = __DIR__ . 'views/address-edit.php';
    			breac;
    
    		case 'view-list' :
    			$template = __DIR__ . 'views/address-view.php';
    			breac;
    
    		default :
    			$template = __DIR__ . 'views/address-list.php';
    			breac;			
    	}
    
    	return $template;
    }

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