Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 949
fri3drice Avatar asked Jul 27 '26 13:07

fri3drice


2 Answers

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 !!

like image 155
Bhautik Nada Avatar answered Jul 30 '26 01:07

Bhautik Nada


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.

like image 40
Gavin Simpson Avatar answered Jul 30 '26 01:07

Gavin Simpson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!