I am building a plugin. I just want to create page template whenever i install plugin. Page template like full-width template. I have used codes after searching but not able to get success. So please help me.

Thanks in advance

  • 2
    what code did you try? – Mark Kaplun Oct 31 '13 at 11:01
  • If you say you tried something and don't show what, the risk is someone offering you the same solution as you already tried. Apart, just asking "I want this, please help me" is considered a work-for-me-for-free type of Question. I'm sure it's not your intention, but it translates like that. – brasofilo Oct 31 '13 at 13:50

If what you want is to actually generate a page template when the plugin is activated, you can use something like this:

 * Plugin Name: Testtest
 * Description: Ignore
 * Version: 0.1
 * Author: windyjonas
function myplugin_activate() {
    $buf = "<?php\n"
        . "/*\n"
        . " * Template Name: generated template\n"
        . " */\n"
        . "?>\n"
        . "Hello world\n";

    $handle = fopen( get_stylesheet_directory() . '/tpl-plugin.php', 'w' );
    fwrite( $handle, $buf );
    fclose( $handle );
register_activation_hook( __FILE__, 'myplugin_activate' );

This only works if the web server user has enough privileges to write to theme directory.

  • This won't scale well... I use the technique from this plugin: copy the template from plugin folder to theme folder. – brasofilo Oct 31 '13 at 13:48
  • Won't scale? It is a one time operation! Doesn't matter if you write to file or copy a file. – windyjonas Oct 31 '13 at 20:12
  • I guess we mean different thing by "scale".... I'm referring to hundreds of lines with .= ? – brasofilo Nov 1 '13 at 5:05
  • Ah, ok, agreed, this should be seen more as a proof of concept, how the content is generated is up to the plugin author. – windyjonas Nov 1 '13 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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