This is an extension of this question/answer. A comment on that question references that template_include
is preferred to template_redirect
when loading templates from a plug-in.
However, hanging this to template_include is breaking everything on the site (any other page view) except the layouts that using the layout override http://pastebin.com/LWpSrfim but using template_include just prepends content (to any template files placed in the theme directory override) — the standard single view for example is still output below.
Edit: source code
<?php
class moulding_templates {
public static function determine_template(){
global $post,$wp_query;
// custom post types
$post_type_array = array('moulding_profiles','moulding_combination','moulding_collection','idea_gallery');
foreach ($post_type_array as $post_type_single) {
global $post;
if (is_singular($post_type_single)) {
moulding_templates::loadSingleTemplate($post);
}
}
// custom taxonomies
$taxonomy_array = array('profile_categories','combination_categories','wood_types');
foreach ($taxonomy_array as $taxonomy_single) {
if (is_tax($taxonomy_single)) {
moulding_templates::loadTaxonomyTemplate($taxonomy_single);
}
}
}
private static function loadSingleTemplate($post) {
$template_name="moulding-templates/single-".$post->post_type.'.php';
$template = locate_template(array($template_name), true);
if(empty($template)) {
include(MOULDINGS_BASE_DIR . 'includes/' . $template_name);
exit();
}
}
private static function loadTaxonomyTemplate($taxonomy) {
$template_name="moulding-templates/taxonomy-".$taxonomy.'.php';
$template = locate_template(array($template_name), true);
if(empty($template)) {
include(MOULDINGS_BASE_DIR . 'includes/' . $template_name);
exit();
}
}
}
add_action('template_include', array('moulding_templates', 'determine_template'));