Skip to content

Instantly share code, notes, and snippets.

@azanebrain
Created January 8, 2014 03:23
Show Gist options
  • Save azanebrain/8311272 to your computer and use it in GitHub Desktop.
Save azanebrain/8311272 to your computer and use it in GitHub Desktop.
Add template files to wordpress plugin
//Template fallback
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
global $wp;
$plugindir = dirname( __FILE__ );
//A Specific Custom Post Type
if ($wp->query_vars["post_type"] == 'product') {
$templatefilename = 'single-product.php';
//A Custom Taxonomy Page
} elseif ($wp->query_vars["taxonomy"] == 'product_categories') {
$templatefilename = 'taxonomy-product_categories.php';
//A Simple Page
} elseif ($wp->query_vars["pagename"] == 'somepagename') {
$templatefilename = 'page-somepagename.php';
}
//First, see if the template is in the theme. If not, use the theme in the plugin directory
if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect($return_template);
}
function do_theme_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment