Is it possible for a plugin to register a page template file?

Is there a way for a plugin to house page template files and have them available after plugin activation?

E.g, I have a plugin that requires specific page page templates to be used, so I’d like them to be available in the Page Attributes meta box in the Editor. But in an attempt to keep my plugin compartmentalized and theme-independent, I’d like to keep the needed page template files out of the theme directory and in a sub-folder of the plugin. I know WordPress parses the theme directory looking for page templates, but is there a way to register templates outside the theme directory?

Related posts

Leave a Reply

1 comment

  1. The easiest & most sensible way I can think of, if you want the theme and plugin to be as independent as possible, is to copy the custom page template files from your plugin’s directory to the current theme directory when the plugin is activated, and delete them when the plugin is deactivated, using register_activation_hook() and register_deactivation_hook().

    That way you wouldn’t have to mess with the template dropdown in the page editor, and you wouldn’t have to hook into template_redirect.