Reputation: 77
I have created a template file for a custom page inside wordpress plugin directory but I can't find the right path to it. This piece of code doesn't work:
update_post_meta( $pas_tasks_page_id, '_wp_page_template', dirname( __FILE__ ) . '/task-list-template.php' );
It works only when I put manually the template file into wordpress theme and changing code to:
update_post_meta( $pas_tasks_page_id, '_wp_page_template', '/task-list-template.php' );
But as a plugin developer I would like to create new template inside my plugin directory not manually. How should I do that?
Upvotes: 2
Views: 3411
Reputation: 448
I recently did something just like this using the "template_include" filter. I did it like this:
function include_template_files() {
$plugindir = dirname( __FILE__ );
if (is_post_type_archive( 'post-type-name' )) {
$templatefilename = 'archive-post_type_name.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
if ('post-type-name' == get_post_type() ){
$templatefilename = 'single-post-type-name.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
}
add_filter( 'template_include', 'include_template_files' );
I just used wordpress conditionals to check what what template was being requested and then created a "theme_files" folder in my plugin directory and placed the appropriately named wordpress template files in there. This was to create a single and archive template for a custom post type.
Upvotes: 5