Skip to content

Instantly share code, notes, and snippets.

@aaroneaton
Last active October 9, 2015 10:37
Show Gist options
  • Save aaroneaton/3489926 to your computer and use it in GitHub Desktop.
Save aaroneaton/3489926 to your computer and use it in GitHub Desktop.
WP:Include template files from plugin
<?php
// In this situation, template files for your custom post type will live in the root of your plugin directory.
// Ideally they would be in a subdirectory.
add_filter( 'archive_template', 'get_archive_template' );
function get_archive_template( $archive_template ) {
global $post;
$plugindir = dirname( __FILE__ );
if( get_query_var( 'post_type' ) == 'your_custom_post_type' ) {
$archive_template = $plugindir . '/custom_post_type_archive.php';
}
return $archive_template;
}
add_filter( 'search_template', 'get_search_template' );
function get_search_template( $search_template ) {
global $post;
$plugindir = dirname( __FILE__ );
if( get_query_var( 'post_type' ) == 'your_custom_post_type' ) {
$search_template = $plugindir . '/custom_post_type_archive.php';
}
return $search_template;
}
add_filter( 'single_template', 'get_single_template' );
function get_single_template( $single_template ) {
global $post;
$plugindir = dirname( __FILE__ );
if( get_query_var( 'post_type' ) == 'your_custom_post_type' ) {
$single_template = $plugindir . '/custom_post_type_single.php';
}
return $single_template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment