Codex

Plugin API/Filter Reference/single template

This filter has been deprecated. That means it has been replaced by a new function or is no longer supported, and has been removed as of WordPress 3.4. All code that uses this function should be converted to use its replacement if one exists. See also wp-includes/deprecated.php. Use '{$type}_template' or template_include instead.

Intro

In 3.3x and previous versions:

The "single_template" filter can be used to load a custom template for a given post or page. It will replace the template used whenever the "single" template is called.

A plugin can register as a content filter with the code:

<?php add_filter( "single_template", "plugin_function_name" ); ?>

Where "plugin_function_name" is the function WordPress should call when the content is being retrieved. Note that the filter function the plugin defines must return the a full path to a template file or the resulting page will be blank. The template file should have the same entries as the "single.php" file has in the theme.

This is especially useful for plugins using the new Custom Post Types to load template files specific to your new post type.

Example with Custom Post Type

<?php
function get_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'my_post_type') {
          $single_template = dirname( __FILE__ ) . '/post-type-template.php';
     }
     return $single_template;
}

add_filter( "single_template", "get_custom_post_type_template" ) ;
?>

The example code will load the template file "post-type-template.php" located in your plugins folder for any posts or pages that have the type of 'my_post_type' else uses default template.

Related

See also index of Function Reference and index of Template Tags.