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.

I need to create a plugin that makes custom page templates available in wp-admin. I'm wondering if someone has tackled this already, as it seems like a pretty typical process?

share|improve this question
    
I don't quite understand the question. You want a plugin that inserts custom templates defined in plugin? –  Steven Feb 13 '11 at 15:17

4 Answers 4

I'm not entirely sure I understand what you are trying to achieve, at least why you would want a plugin to do that.

The normal procedure for creating different page templates is:

  1. Create a new page-template in your ACTIVE theme directory (make a copy of page.php).

  2. Change the name of the template (inside the file).

    /* Template Name: Full Width Page */

  3. Change the code of the page to what you are trying to achieve.

  4. You can now go create a new page and choose which "Template" to use.

alt text

...

I hope that is what you are trying to achieve?

Official documentation here: http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

share|improve this answer
    
Woops. sample PHP code is a bit messed up. Visit the link for proper code example. –  Lars Koudal Nov 16 '10 at 18:21
2  
Interested in knowing if anyone is using Jan's method in an existing plugin: wordpress.stackexchange.com/questions/3396/… –  jnthnclrk Nov 17 '10 at 10:26
    
I believe @trnsfrmr is looking for a way how to create a pre-defined page template (steps 1-3) inside his own plugin to make this template available for users after the plugin activation. I suppose he wants to keep this solution theme independent. –  Michal Mau Feb 7 '11 at 16:26
    
Ah, okay, I misunderstood a bit there, thanks for the clarification :-) –  Lars Koudal Feb 7 '11 at 20:32

This seems to be quite hard to achieve. get_page_templates() function actively discards anything that is not located in parent and child root directories. It also doesn't store in global variable or allow to filter generated list of templates.

I think page attributes meta box will need to be forked and completely replaced for this. And not sure it will be possible even then.

I agree that this seems like something that makes sense, but WordPress code is very precise in that it wants named templates to only come from theme's directory.

share|improve this answer

Like Rarst answered you can really do that without editing core files or remove the page attributes metabox and create your on using the same code with a bit of modification. the code below is the code for the /admin/include/meta-boxes.php and i added the a comment to show where your extra page template options would go:

function page_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
        $pages = wp_dropdown_pages(array('post_type' => $post->post_type, 'exclude_tree' => $post->ID, 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
        if ( ! empty($pages) ) {
        ?>
        <p><strong><?php _e('Parent') ?></strong></p>
        <label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
        <?php echo $pages; ?>
        <?php
        } // end empty pages check
    } // end hierarchical check.
    if ( 'page' == $post->post_type && 0 != count( get_page_templates() ) ) {
        $template = !empty($post->page_template) ? $post->page_template : false;
        ?>
        <p><strong><?php _e('Template') ?></strong></p>
        <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
        <option value='default'><?php _e('Default Template'); ?></option>
        <?php page_template_dropdown($template); ?>

        // add your page templates as options

        </select>
        <?php
    } ?>
    <p><strong><?php _e('Order') ?></strong></p>
    <p><label class="screen-reader-text" for="menu_order"><?php _e('Order') ?></label><input name="menu_order" type="text" size="4" id="menu_order" value="<?php echo esc_attr($post->menu_order) ?>" /></p>
    <p><?php if ( 'page' == $post->post_type ) _e( 'Need help? Use the Help tab in the upper right of your screen.' ); ?></p>
    <?php
}

Don't know if this is a fix in your case but i had a smiler case when i needed to display the post type in a plugin built-in theme and for that i used add_filter('the_content', 'my-function'); and my-function created the output to display.

Another option would be to make your plugin create the template file in the current theme directory , something like this:

function create_plugins_theme_file(){
    $file_name = TEMPLATEPATH . '/' . $tamplate_name . '.php';
    $handle = fopen($file_name, 'w') or wp_die('Cannot open file for editing');
    $file_contents = <<<OUT
<?php
/*
Template Name: $tamplate_name
*/
?>

//you theme file here

OUT;

   fwrite($handle, $file_contents);
   fclose($handle);
 }

and you can run this after you first check if the file exist

if(!file_exists( $file_name)){create_plugins_theme_file();}

Hope one of this helps.

share|improve this answer
    
Will test meta box stuff, bus as per my answer - it's only part of issue, other is - code that tries to load template only if it's in theme directory. So providing "external" template file may not be sufficient to get it loaded. –  Rarst Feb 7 '11 at 20:07
    
that's why i suggested to make the plugin create the files in the theme's directory. –  Bainternet Feb 8 '11 at 10:42

Isn't doing this one of the reasons that register_theme_directory() got introduced?

http://core.trac.wordpress.org/ticket/10467

At the time it was an issue related to BuddyPress, which wanted to toss in extra templates here and there.

I take it they're not properly listed as page templates, though:

http://core.trac.wordpress.org/ticket/15803

share|improve this answer
    
I will go over code (probably tomorrow), but as far as I remember templates with names are explicitly looked up through path constants only. –  Rarst Feb 7 '11 at 20:05

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.