• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

Custom Post Type Templates in Plugins

September 12, 2012 by Webhead

Problem

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.  It would be nice if everythere could be contained in the plugin and the theme could override if needed.

Solution

WordPress to the rescue… There is a filter called ‘template_include’ that can be used to set the template.  It’s a general filter so you’ll need to check for your post type.

add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {
    $post_types = array( 'project' );

    if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '/archive-project.php' ) )
        $template = 'path/to/list/template/in/plugin/folder.php';
    if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '/single-project.php' ) )
        $template = 'path/to/singular/template/in/plugin/folder.php';

    return $template;
}

source: http://wordpress.stackexchange.com/questions/55763/is-it-possible-to-define-a-template-for-a-custom-post-type-within-a-plugin-indep
keywords:  plugin template custom post type


Filed Under: Coding Tagged With: php, wordpress

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools