Is it possible to define a template for a custom post type within a plugin independent of the active theme?

You need to use the template_include filter which is the generic filter for all template inclusions.

add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {
    $post_types = array( 'project' );

    if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '/archive-project.php' ) )
        $template="path/to/list/template/in/plugin/folder.php";
    if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '/single-project.php' ) )
        $template="path/to/singular/template/in/plugin/folder.php";

    return $template;
}

I’ve not fully tested the post type archive bit, you may need to include a check using is_tax( $taxonomies ) to get it to work on associated custom taxonomy archives.

Leave a Comment