• I am writing a few plugins which require Page Templates;

    1) Do these have to be installed in the current theme (and others if changed) or is there a shared place I can put them? If require to be installed in each theme then what is the standard / recommended method?

    2) Is it best to use the plugin to create and write the files out or is there an inbuilt method for placing the templates?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Some good questions!

    For the templates to show up in the dropdown on the Page Edit Screen, the files must reside in the current theme’s folder (sort of, more on this later). If that is not a requirement, then the template files can reside anywhere. After the page is created using the default template, alter the post meta entry for the template name to the correct path to the template file.

    While it’s possible to use basic PHP file system commands to copy, move, or create files in the current theme folder, I think it’s a bad idea. The plugin would have to keep track of what the current theme is. Unless done carefully, you could end up littering the themes folders with many copies of your files. It’s not fool proof to clean up after yourself if the plugin should be deleted. Finally, IMO, it’s just plain rude to write files into the file space of other entities.

    Fortunately, one clever person figured out how to fool the WP caching system into thinking the template files reside in the theme folder while they actually are safely stored in your own plugin folder. The files appear in the dropdown just like ones in the theme. I can’t find the online source of this plugin, but I do have a copy I can post it if you’re interested. It did have one flaw where it didn’t work on the quick edit screen, but I figured out the fix for that. The version I have includes this fix of course. I haven’t tested this plugin in quite some time, so I don’t know if it still works with the latest WP release.

    Thread Starter rabbitFoot

    (@rabbitfoot)

    That’s the same reasoning which led me to ask, I would surely like to see what code you have on this.

    Many thanks

    Moderator bcworkz

    (@bcworkz)

    I can’t get to my copy of that plugin right now. I’ll post a link here the first part of next week once I regain access. Sorry for the delays, I’m having some connectivity issues.

    If it’s a plugin then your templates should reside within your plugin directory. Here’s a helpful code to make your plugin templates work.

    add_action( ‘wp_loaded’, ‘add_my_templates’ );
    function add_my_templates(){
    if( is_admin() ){
    global $wp_object_cache;
    $current_theme = wp_get_theme();
    $template = $current_theme->get_page_templates();
    $hash = md5( $current_theme->theme_root . ‘/’. $current_theme->stylesheet );
    $templates = $wp_object_cache->get( ‘page_templates-‘. $hash, ‘themes’ );
    $templates[‘mytemplates/test-template.php’] = __(‘Your Template Name’);
    wp_cache_replace( ‘page_templates-‘. $hash, $templates, ‘themes’ );
    }
    else {
    add_filter( ‘page_template’, ‘get_my_template’, 1 );
    }
    }

    function get_my_template( $template ){
    $post = get_post();
    $page_template = get_post_meta( $post->ID, ‘_wp_page_template’, true );
    if( $page_template == ‘mytemplates/test-template.php’ ){
    $template = YOUR_PLUGIN_DIRECTORY . “/mytemplates/test-template.php”;
    }
    return $template;
    }

    Good luck!

    Moderator bcworkz

    (@bcworkz)

    Thanks for sharing irenem!

    FWIW, here is the code I was referring to: http://pastebin.com/t5KukZL9

    Just like irenem’s script, it alters the WP cache so templates residing elsewhere show up as though the template were in the theme folder. Also similar is you must hardcode the name of your template in the code, you don’t want to run inefficient code that searches for your template files on each page load 🙂

    The version I have is obviously more extensive, I can’t pretend to understand all of what is going on, it’s obviously not all necessary, but perhaps it results in a more robust implementation. You don’t really need to install this as a separate plugin (though that is the intention). You could include the file in your own plugin code if desired, but please leave the attribution and license data at the top intact.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Plugin install Page Templates’ is closed to new replies.