0

Right now i am developing a plugin for one of my client according to their need. But I want the plugin hold all the functionality itself including the custom template. I mean my plugin holds the custom template but i don't know that if the template display on the admin post/page section.

Is it possible to display custom template on admin post/page section through plugin? If yes then how? Your help is highly appreciated.

UPDATE:

I have achieved this with old method like create a separate custom template for my plugin so that admin can select the template for the desired plugin. I don't want to provide two separate files for my client, one for plugin and second for theme :(

3
  • This doesn't make much sense. what template, where is it used, and what have you tried? Nov 19, 2013 at 9:39
  • @MarkKaplun, I have tried the old method, like create a custom template in the theme directory and then select that template from the Add/Edit Page's template section. But what if i put that custom template on my plugin? And how do i display my custom template on Edit Page section after putting the custom template on plugin directory? Hope you understand :(
    – jogesh_pi
    Nov 19, 2013 at 9:54
  • so you are talking about page templates? can you please edit the question to make it clear. Nov 19, 2013 at 10:02

2 Answers 2

1

Page templates are limited to themes by design. Page templates belong to the presentation layer while plugins should do very limited and focused presentation as they can't "know" the presentation implications of their output.

But if you have to do it in a plugin, the better way is to add another box to the page editing screen in which the user can select to use the plugin's temlate for that age, store the value in a meta data and do something like

add_filter('page_template','wpse1223720_template');

function wpse1223720_template($temlate) {
  global $post;

  if (get_post_meta($post->ID,your telmlate meta key) == 'use my temlate')
    return the path to your template file.
  else 
   return $template;
}

Not tested at all. This code interrupts the code that find the appropriate template file to use if the content being served is a page, and makes wordpress use your own template instead of the one associated with the page.

Alternative approach is to run your hook on the template_redirect action and run "manually" your template. You can see and example for this here https://stackoverflow.com/questions/4647604/wp-use-file-in-plugin-directory-as-custom-page-template

3
  • according to get_post_meta($post->ID,your telmlate meta key) == 'use my temlate', can i have to assign a meta key for the post?
    – jogesh_pi
    Nov 19, 2013 at 10:53
  • yes, that is the first part of the answer, you have to add some selection box or checkbox to the page editing age so the user can select when to use your template and which one, if there are more then one. This is the codex explanation on how to do it - codex.wordpress.org/Function_Reference/add_meta_box Nov 19, 2013 at 11:24
  • Ahh, i think this idea is great. Thanks Mark, I will implement this on my plugin.
    – jogesh_pi
    Nov 19, 2013 at 11:29
1

Template are usually relegated to theme development and are used to provide layout and presentation which is precisely what themes are meant to provide.

However, you can add a template file to theme when the plugin is activated. You can hook the plugin activation (and deactivation)

register_activation_hook($filename, 'myPluginActivation');
register_deactivation_hook($filename, 'myPluginDeactivation');

and manually create the template files in the theme directory using fopen() fwrite() fclose() (Remember to delete created file on plugin deactivation).

The knows issues of this strategy are:

  • if the user change theme the template file doesnt move to new theme directory
  • there are no error handling for file operations
  • the files that you're giong to create can already exist

If you do not need to leave the choice of template at the client, you can use the template redirect hook and manually redirect the default template with the one in plugin dir.

add_action("template_redirect", 'my_theme_redirect');

function my_theme_redirect() {
  $plugindir = dirname( __FILE__ );
  $return_template = $plugindir . '/themefiles/' . $templatefilename;
  do_theme_redirect($return_template);
}

In this way you override the default template when the plugin is activated. Personally I would use the plugin to create a custom post type and a redirect strategy just for that post type, checking if the custom post type template already exists in theme dir to leave the possibility to create a custom post type template in the theme dir.

You can find useful information in this Tom McFarlin article

6
  • This is what i want to know, Many thanks, But is there some way to display $plugindir . '/themefiles/' . $templatefilename; on Edit Page's template section? so that the default template not override and user can select the template for the page?
    – jogesh_pi
    Nov 19, 2013 at 9:59
  • I think that the only way is to manually create the template files in theme dir. Consider to develop a theme with the plugin's functionalities. Nov 19, 2013 at 10:02
  • I can't create a separate theme, actually i have to provide this plugin to my client and i don't want to give him two separate files one for plugin and second to put that file on theme directory.
    – jogesh_pi
    Nov 19, 2013 at 10:06
  • I probably badly explained using the word 'manually'. Hook the plugin activation with a function that create a new file in theme's directory. This new file will be the custom template (using the appropriate syntax). In this way you can give to your client just one file that creates the template that he can select as template for the page. Read the article that I linked. Nov 19, 2013 at 10:14
  • Thanks for the link, i am still reading and got lot of new stuff to learn more. Thanks :)
    – jogesh_pi
    Nov 19, 2013 at 10:30

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.