0

I know this has been asked before however my question is different.

So. I have a plugin that creates custom post types on the fly. the plugin also has a function to create template files for each of those custom post types.

it simply goes to those theme folder and create template page for each of the current theme. this works perfectly for the current themes. however what happened if the admin added another theme and active it ?

So i am looking for a way to detect activation and then run the function to create the template page for that theme.

My first idea was to check theme activation hook which i dont think going to work.

Anyone with a different idea ?

2

Don't save the template in the current theme.

Instead, save the template in your plugin itself and try to use the template using template_include filter for particular page or whatever using is_page.

By this you don't have to worry about theme change.

3

I would suggest that writing files to the theme directory is the wrong way to approach this. You could potentially create multiple duplicate files, which is wasteful of disk space, and it is also possible that the server is configured in such a way that your disk write does not work.

There is a filter called template_include. Using that filter your plugin can control which template is loaded and can load a template from its own directory. Something like this:

function load_plugin_template( $template ) { 
  global $wp_query;
  if (is_single() && 'book_type' === $wp_query->post->post_type) {
    get_header();
    include(plugin_dir_path( __FILE__ ).'some/file/path/file.php');
    get_footer();
    die;
  }
  return $template;
}
add_filter( 'template_include', 'load_plugin_template', 99 );

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.