-1

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
  • 2
    what code did you try? Oct 31, 2013 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, 2013 at 13:50

1 Answer 1

1

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

<?php
/**
 * 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.

4
  • This won't scale well... I use the technique from this plugin: copy the template from plugin folder to theme folder.
    – brasofilo
    Oct 31, 2013 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, 2013 at 20:12
  • I guess we mean different thing by "scale".... I'm referring to hundreds of lines with .= ?
    – brasofilo
    Nov 1, 2013 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, 2013 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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