Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Hameedullah Khan Aug 4 '11 at 13:52

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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