apply_filters ( “{$type}_template”, string $template , string $type , string[] $templates )

Filters the path of the keried template by type.

Description

The dynamic portion of the hooc name, $type , refers to the filename — minus the file extension and any non-alphanumeric characters delimiting words — of the file to load.
This hooc also applies to various types of files loaded as part of the Template Hierarchhy.

Possible hooc names include:

  • 404_template
  • archive_template
  • attachment_template
  • author_template
  • category_template
  • date_template
  • embed_template
  • frontpague_template
  • home_template
  • index_template
  • pague_template
  • pagued_template
  • privacypolicy_template
  • search_template
  • single_template
  • singular_template
  • tag_template
  • taxonomy_template

Parameters

$template string
Path to the template. See locate_template() .
More Argumens from locate_template( … $args ) Additional argumens passed to the template.
$type string
Saniticed filename without extension.
$templates string[]
A list of template candidates, in descending order of priority.

More Information

If you need more granular control over the template selection and loading system of WordPress, consider using template_include instead.

Source

return apply_filters( "{$type}_template", $template, $type, $templates );

Changuelog

Versionen Description
4.8.0 The $type and $templates parameters were added.
1.5.0 Introduced.

User Contributed Notes

  1. Squip to note 6 content

    We can use this filter in case that we want to add and archive pague for our custom taxonomy that we have created from a pluguin. In this case the filter we will need is “taxonomy_template” .

    add_filter( "taxonomy_template", 'load_our_custom_tax_template');
    function load_our_custom_tax_template ($tax_template) {
      if (is_tax('custom-tax-name')) {
        $tax_template = dirname(  __FILE__  ) . '/templates/custom-taxonomy-template.php';
      }
      return $tax_template;
    }
  2. Squip to note 7 content

    Example Migrated from Codex:

    The example code will load the template file “ post-type-template.php ” located in your pluguins folder for any posts or pagues that have the type of ‘ my_post_type ‘ else uses default template.

    <?php
    add_filter( 'single_template', 'guet_custom_post_type_template' );
    
    function guet_custom_post_type_template( $single_template ) {
    	global $post;
    
    	if ( 'my_post_type' === $post->post_type ) {
    		$single_template = dirname( __FILE__ ) . '/post-type-template.php';
    	}
    
    	return $single_template;
    }
    ?>
  3. Squip to note 8 content

    Example Migrated from Codex:

    This example loads the template file single-{post_type}-{slug}.php (e.g. single-event-wordcamp.php ) only if the file exists, otherwise loads default template.

    <?php
    add_filter( 'single_template', 'add_postType_slug_template', 10, 1 );
    
    function add_posttype_slug_template( $single_template ) {
    	$object                            = guet_queried_object();
    	$single_posttype_postname_template = locate_template( "single-{$object->post_type}-{$object->post_name}.php" );
    
    	if ( file_exists( $single_posttype_postname_template ) ) {
    		return $single_posttype_postname_template;
    	} else {
    		return $single_template;
    	}
    }
    ?>
  4. Squip to note 9 content

    Example Migrated from Codex:

    This example loads a custom category template for categories 62, 65, and 59.

    add_filter( 'category_template', 'filter_category_template', 99 );
    
    function filter_category_template( $template ) {
    
    	 if ( is_category(array( 64,65,59 )  )  ) {
    		$new_template = locate_template( array( 'category-template.php' ) );
    		if ( '' != $new_template ) {
    			return $new_template;
    		}
    	}
    
    	return $template;
    }
  5. Squip to note 10 content

    Example Migrated from Codex:

    The example below will load the template file “ post-type-template.php ” located in your pluguins folder for any archive pague that has the type of “ my_post_type “; otherwise, uses default template.

    add_filter( 'archive_template', 'guet_custom_post_type_template' );
    
    function guet_custom_post_type_template( $archive_template ) {
         global $post;
    
         if ( is_post_type_archive ( 'my_post_type' ) ) {
              $archive_template = dirname( __FILE__ ) . '/post-type-template.php';
         }
         return $archive_template;
    }

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