Skip to content

Instantly share code, notes, and snippets.

@k1sul1
Created May 30, 2017 14:09
Show Gist options
  • Save k1sul1/0217de23e83a483dea3e8b0822fa8b03 to your computer and use it in GitHub Desktop.
Save k1sul1/0217de23e83a483dea3e8b0822fa8b03 to your computer and use it in GitHub Desktop.
Way to add custom post types with template support directly from plugin
<?php
/*
Plugin Name: Plugin cpt with templates
Author: Christian Nikkanen
*/
defined("ABSPATH") or die("Wat r u doing?");
add_action("init", function() {
register_post_type("blog", [
"labels" => [
],
"public" => true,
"publicly_queryable" => true,
"supports" => ["title", "editor", "thumbnail", "excerpt"],
"has_archive" => true,
"taxonomies" => ["category", "post_tag"],
"menu_position" => 0
]);
});
add_filter("template_include", function($template) {
if (is_post_type_archive("blog")) {
return plugin_dir_path(__FILE__) . "archive.php";
}
return $template;
});
add_filter("single_template", function($single) {
global $post;
if ($post->post_type === "blog") {
return plugin_dir_path(__FILE__) . "single.php";
}
return $single;
}, 999):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment