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 it possible to make custom page templates available from a plugin?

share|improve this question

1 Answer 1

up vote 32 down vote accepted

get_page_template() can be overridden via the page_template filter. If your plugin is a directory with the templates as files in them, it's just a matter of passing the names of these files. If you want to create them "on the fly" (edit them in the admin area and save them in the database?), you might want to write them to a cache directory and refer to them, or hook into template_redirect and do some crazy eval() stuff.

A simple example for a plugin that "redirects" to a file in the same plugin directory if a certain criterium is true:

add_filter( 'page_template', 'wpa3396_page_template' );
function wpa3396_page_template( $page_template )
{
    if ( is_page( 'my-custom-page-slug' ) ) {
        $page_template = dirname( __FILE__ ) . '/custom-page-template.php';
    }
    return $page_template;
}
share|improve this answer
    
Awesome! I'll try the former option. –  jnthnclrk Nov 5 '10 at 13:39
    
Hey Jan, do you have some example code on how to pass a plugin file in as a custom page template? –  jnthnclrk Nov 16 '10 at 17:58
    
@trnsfrmr: It's really easy, I added a simple example to my answer. –  Jan Fabry Nov 16 '10 at 19:46
    
thanks, will try that! –  jnthnclrk Nov 17 '10 at 12:56
3  
Note that this has been more or less replaced by the "template_include" filter in later versions (3.1+). –  Inigoesdr Jun 10 '13 at 15:54

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.