Template are usually relegated to theme development and are used to provide layout and presentation which is precisely what themes are meant to provide.
However, you can add a template file to theme when the plugin is activated. You can hook the plugin activation (and deactivation)
register_activation_hook($filename, 'myPluginActivation');
register_deactivation_hook($filename, 'myPluginDeactivation');
and manually create the template files in the theme directory using fopen()
fwrite()
fclose()
(Remember to delete created file on plugin deactivation).
The knows issues of this strategy are:
- if the user change theme the template file doesnt move to new theme directory
- there are no error handling for file operations
- the files that you're giong to create can already exist
If you do not need to leave the choice of template at the client, you can use the template redirect hook and manually redirect the default template with the one in plugin dir.
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
$plugindir = dirname( __FILE__ );
$return_template = $plugindir . '/themefiles/' . $templatefilename;
do_theme_redirect($return_template);
}
In this way you override the default template when the plugin is activated.
Personally I would use the plugin to create a custom post type and a redirect strategy just for that post type, checking if the custom post type template already exists in theme dir to leave the possibility to create a custom post type template in the theme dir.
You can find useful information in this Tom McFarlin article