{"id":857,"date":"2012-09-12T10:31:10","date_gmt":"2012-09-12T20:31:10","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=857"},"modified":"2012-09-12T10:31:10","modified_gmt":"2012-09-12T20:31:10","slug":"custom-post-type-templates-in-plugins","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/","title":{"rendered":"Custom Post Type Templates in Plugins"},"content":{"rendered":"

Problem<\/p>\n

I made a plugin that created a custom post type, but the only way I could customize the single-customposttype.php and archive-customposttype.php is by placing the files into the theme. \u00a0It would be nice if everythere could be contained in the plugin and the theme could override if needed.<\/p>\n

Solution<\/p>\n

WordPress to the rescue… There is a filter called ‘template_include’ that can be used to set the template. \u00a0It’s a general filter so you’ll need to check for your post type.<\/p>\n

add_filter( 'template_include', 'my_plugin_templates' );\nfunction my_plugin_templates( $template ) {\n\u00a0 \u00a0 $post_types = array( 'project' );\n\n\u00a0 \u00a0 if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '\/archive-project.php' ) )\n\u00a0 \u00a0 \u00a0 \u00a0 $template = 'path\/to\/list\/template\/in\/plugin\/folder.php';\n\u00a0 \u00a0 if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '\/single-project.php' ) )\n\u00a0 \u00a0 \u00a0 \u00a0 $template = 'path\/to\/singular\/template\/in\/plugin\/folder.php';\n\n\u00a0 \u00a0 return $template;\n}<\/code><\/pre>\n

source:\u00a0http:\/\/wordpress.stackexchange.com\/questions\/55763\/is-it-possible-to-define-a-template-for-a-custom-post-type-within-a-plugin-indep<\/a>
\nkeywords: \u00a0plugin template custom post type<\/p>\n

<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[54,64],"yoast_head":"\nCustom Post Type Templates in Plugins - My Monkey Do<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Post Type Templates in Plugins - My Monkey Do\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/\" \/>\n<meta property=\"og:site_name\" content=\"My Monkey Do\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-12T20:31:10+00:00\" \/>\n<meta name=\"author\" content=\"Webhead\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Webhead\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/\",\"url\":\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/\",\"name\":\"Custom Post Type Templates in Plugins - My Monkey Do\",\"isPartOf\":{\"@id\":\"https:\/\/mymonkeydo.com\/#website\"},\"datePublished\":\"2012-09-12T20:31:10+00:00\",\"dateModified\":\"2012-09-12T20:31:10+00:00\",\"author\":{\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\"},\"breadcrumb\":{\"@id\":\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mymonkeydo.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom Post Type Templates in Plugins\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mymonkeydo.com\/#website\",\"url\":\"https:\/\/mymonkeydo.com\/\",\"name\":\"My Monkey Do\",\"description\":\"A Log of Coding Solutions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mymonkeydo.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\",\"name\":\"Webhead\",\"url\":\"https:\/\/mymonkeydo.com\/author\/corey\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom Post Type Templates in Plugins - My Monkey Do","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/","og_locale":"en_US","og_type":"article","og_title":"Custom Post Type Templates in Plugins - My Monkey Do","og_url":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/","og_site_name":"My Monkey Do","article_published_time":"2012-09-12T20:31:10+00:00","author":"Webhead","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Webhead","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/","url":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/","name":"Custom Post Type Templates in Plugins - My Monkey Do","isPartOf":{"@id":"https:\/\/mymonkeydo.com\/#website"},"datePublished":"2012-09-12T20:31:10+00:00","dateModified":"2012-09-12T20:31:10+00:00","author":{"@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829"},"breadcrumb":{"@id":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mymonkeydo.com\/custom-post-type-templates-in-plugins\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mymonkeydo.com\/"},{"@type":"ListItem","position":2,"name":"Custom Post Type Templates in Plugins"}]},{"@type":"WebSite","@id":"https:\/\/mymonkeydo.com\/#website","url":"https:\/\/mymonkeydo.com\/","name":"My Monkey Do","description":"A Log of Coding Solutions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mymonkeydo.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829","name":"Webhead","url":"https:\/\/mymonkeydo.com\/author\/corey\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/857"}],"collection":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/comments?post=857"}],"version-history":[{"count":0,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/857\/revisions"}],"wp:attachment":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/media?parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/categories?post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/tags?post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}