Created
May 30, 2017 14:09
-
-
Save k1sul1/0217de23e83a483dea3e8b0822fa8b03 to your computer and use it in GitHub Desktop.
Way to add custom post types with template support directly from plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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