'Add custom post type template via plugin

I'm creating a plugin for a custom post type. I want to add a custom template for it. But I'm not sure how to add it via the plugin.

How can I add a custom post type template via the plugin?

Please help!



Solution 1:[1]

You can simply create and assign custom page templates for your custom post type in your custom plugin.

Just create 2 template file - single-{post_type}.php and archive-{post_type}.php - in a new templates sub-directory of your plugin directory.

Then add some code as per below example in your main plugin:

/*
 * Set Page templates for CPT "your_cpt"
 */


add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {

    $post_type = 'your_cpt'; // Change this to the name of your custom post type!

    if ( is_post_type_archive( $post_type ) && file_exists( plugin_dir_path(__DIR__) . "templates/archive-$post_type.php" ) ){
        $template = plugin_dir_path(__DIR__) . "templates/archive-$post_type.php";
    }

    if ( is_singular( $post_type ) && file_exists( plugin_dir_path(__DIR__) . "templates/single-$post_type.php" ) ){
        $template = plugin_dir_path(__DIR__) . "templates/single-$post_type.php";
    }

    return $template;
}

Hope this example would be helpful for you. Cheers !!

Solution 2:[2]

Below is a complete (blank) plugin that I put together based on my previously posted answer. I loaded it in my theme (based on twentyfifteen) and it works. Together with the plugin, as-is, you would also need The following file structure

 - schs-blank/css schs-blank/js schs-blank/images
   schs-blank/archive-blank.php schs-blank/single-blank.php
   schs-blank/schs-blank-edit-posts.php
   schs-blank/schs-blank-template.php

It should not be too tricky to figure out what should be in the above files, but for starters just put "<h1>{filename}</h1>" to see where each page is called.

<?php
/*
        Plugin Name: SCHS Blank Plugin
        Plugin URI: http://www.southcoasthosting.com/schs-blank
        Tags: jquery, flyout, vertical, menu, animated, css, navigation, widget, plugin, scroll
        Description: Creates a widget to place a vertical menu which caters for many menu items.
        Author: Gavin Simpson
        Version: 0.1
        Author URI: http://www.southcoasthosting.com
        074 355 1881
*/
class schs_blank
{
    protected $plugin_slug;
    private static $instance;
    protected $templates;

    public static function get_instance() 
    {
        if( null == self::$instance )
        {
            self::$instance = new schs_blank();
        }
        return self::$instance;
    } 
    private function __construct()
    {
        $this->templates = array();
        $page_template = dirname( __FILE__ ) . '/schs-blank-template.php';
        $this->templates = array('schs-blank-template.php' => 'SCHS Blank Page Template','schs-blank-edit-posts.php'=>'Edit Blank Post Template');
        if(!is_admin())
        {
            add_action( 'wp_blank', array('schs_blank', 'header') );
        }
        else
            add_action( 'wp_blank', array('schs_blank', 'footer') );

        add_filter('page_attributes_dropdown_pages_args',array( $this, 'register_project_templates' ));
        add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ));
        add_filter( 'template_include', array($this,'schs_force_template' ));
        add_filter('template_include', array( $this, 'view_project_template') );

        add_action('admin_menu', array($this,'setup_blank_admin_menus'));
        add_action( 'init', array($this,'blank_custom_post' ));
    }
    function schs_force_template($template)
    {
        if( is_archive( 'blank' ) )
        {
            $template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/archive-blank.php';
        }
        if( is_singular( 'blank' ) ) 
        {
            $template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/single-blank.php';
        }
        return $template;
    }
    function header(){
        wp_enqueue_script('jquery');
        wp_enqueue_style( 'blank-css', schs_blank::get_plugin_directory() . "/css/blank.css" );
        wp_enqueue_script('schs_blank', schs_blank::get_plugin_directory() . '/js/schs-blank.js', array('jquery'));
    }
    function footer()
    {
    }
    function get_plugin_directory(){
        return WP_PLUGIN_URL . '/schs-blank';   
    }
    function setup_blank_admin_menus()
    {
        add_menu_page('SCHS Blank Settings', 'SCHS Blank Settings', 'manage_options', 
            'SCHS_blank_settings', array($this,'SCHS_page_settings'));
        add_submenu_page('SCHS_blank_settings', 
            'SCHS Page Elements', 'Blank Topics', 'manage_options', 
            'SCHS_blank_topics_settings', array($this,'SCHS_blank_topics_settings')); 
    }
    function SCHS_page_settings()
    {
        ?>
        <div class="wrap">
        <h2>There are no Blank options at this stage.</h2>
        </div>
        <?php
    }
    function SCHS_blank_topics_settings()
    {
        ?>
        <div id="blank_topics" class="wrap">
        <h1>There are no blank topics at this stage</h1>
        </div>
        <?php
    }
    function blank_custom_post()
    {
        $labels = array(
        'name'               => _x( 'Blank', 'post type general name' ),
        'singular_name'      => _x( 'Topic', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Blank Topic' ),
        'edit_item'          => __( 'Edit Blank Topic' ),
        'new_item'           => __( 'New Blank Topic' ),
        'all_items'          => __( 'All Blank Topics' ),
        'view_item'          => __( 'View Blank Topics' ),
        'search_items'       => __( 'Search Blank Topics' ),
        'not_found'          => __( 'No blank topics found' ),
        'not_found_in_trash' => __( 'No blank topics found in the trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Blank'
        );
        $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our blank topic specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor'),
        'has_archive'   => true,
        'menu_icon'     => $this->get_plugin_directory().'/images/blank-icon.png',
        );
        register_post_type( 'blank', $args );
        flush_rewrite_rules();
    }

    public function register_project_templates( $atts )
    {
                // Create the key used for the themes cache
                $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
                // Retrieve the cache list. 
                // If it doesn't exist, or it's empty prepare an array
                $templates = wp_get_theme()->get_page_templates();
                if ( empty( $templates ) ) {
                        $templates = array();
                } 
                // New cache, therefore remove the old one
                wp_cache_delete( $cache_key , 'themes');
                // Now add our template to the list of templates by merging our templates
                // with the existing templates array from the cache.
                $templates = array_merge( $templates, $this->templates );
                // Add the modified cache to allow WordPress to pick it up for listing
                // available templates
                wp_cache_add( $cache_key, $templates, 'themes', 1800 );
                return $atts;
    }

    /**
         * Checks if the template is assigned to the page
         */
    public function view_project_template( $template )
    {
                global $post;
                if (!isset($this->templates[get_post_meta( 
                    $post->ID, '_wp_page_template', true 
                )] ) ) {

                        return $template;

                } 
                $file = plugin_dir_path(__FILE__). get_post_meta( 
                    $post->ID, '_wp_page_template', true 
                );
                // Just to be safe, we check if the file exist first
                if( file_exists( $file ) )
                {
                    return $file;
                } 
                else 
                {
                    echo $file; 
                    exit;
                }
                return $template;
    }

}
add_action( 'plugins_loaded', array( 'schs_blank', 'get_instance' ) );
?>

I hope this helps a bit more.

Solution 3:[3]

This work for me, kindly try it, Thanks
Templates is loaded into cpt file, which was located at
custom_plugin -> app -> cpt -> cpt_article.php
Template is located
custom_plugin -> app -> templates

add_filter( 'single_template', 'load_my_custom_template', 99, 1 );
 function load_custom_templates($single_template) {
   global $post;
   if ($post->post_type == 'article' ) {
   $single_template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'single_article.php';
  }
   return $single_template;
}

Solution 4:[4]

  • You create a template file in your plugin, e.g /templates/myposttype-page.php
  • Then add below code into your plugin.
  • Change 'myposttype' to your post type
$custom_post_type = 'myposttype';
add_filter("theme_{$custom_post_type}_templates", "add_{$custom_post_type}_template");

add_filter('single_template', "redirect_{$custom_post_type}_template");

function add_myposttype_template() {

     $templates['myposttype-page.php'] = 'My type Template';
    
     return $templates;
}

function redirect_myposttype_template ($template) {

      if( is_page_template('myposttype-page.php') ){
        $template = plugin_dir_path(__FILE__). 'templates/myposttype-page.php';
      }

      return $template;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ban-geoengineering
Solution 2
Solution 3
Solution 4