1

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?

1
  • The only way I can think is creating a custom meta box. As the page attribute meta box is very simpler so it won't be difficult. Or maybe hide the template drop down from page attribute meta box and create a dedicated meta box for template selection. Aug 4, 2011 at 13:52

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.