katstar01
3/2/2017 - 6:00 PM

Register custom post type templates in a plugin

Register custom post type templates in a plugin

<?php


add_filter('single_template','KE_sites_template_single');
add_filter('archive_template','KE_sites_template_archive');

//route single- template
function KE_sites_template_single($single_template){
  global $post;
  $found = locate_template('single-template.php');
  if($post->post_type == 'POST TYPE' && $found != ''){
    $single_template = dirname(__FILE__).'/single-template.php';
  }
  return $single_template;
}

//route archive- template
function KE_sites_template_archive($template){
  if(is_post_type_archive('POST TYPE')){
    $theme_files = array('archive-template.php');
    $exists_in_theme = locate_template($theme_files, false);
    if($exists_in_theme == ''){
      return plugin_dir_path(__FILE__) . '/archive-template.php';
    }
  }
  return $template;
}