Skip to content

Instantly share code, notes, and snippets.

@ginsterbusch
Last active August 29, 2015 14:22
Show Gist options
  • Save ginsterbusch/72f5dedfd115501f689b to your computer and use it in GitHub Desktop.
Save ginsterbusch/72f5dedfd115501f689b to your computer and use it in GitHub Desktop.
Example of how to implement separate customizable templates for plugins (similar to how eg. WooCommerce uses them)
$template_name = 'my_template.php';
$load_template = plugin_dir_path(__FILE__) . 'templates/' . $template_name;
/**
* See @link http://codex.wordpress.org/Function_Reference/wp_upload_dir#Usage
*/
if( file_exists( $upload_dir['basedir'] . '/my_plugin_name/' . $template_name ) ) {
$load_template = $upload_dir['basedir'] . '/my_plugin_name/' . $template_name;
} else {
/**
* See @link http://codex.wordpress.org/Function_Reference/load_template#Examples
*/
if ( $overridden_template = locate_template( 'some-template.php' ) ) {
// locate_template() returns path to file
// if either the child theme or the parent theme have overridden the template
//load_template( $overridden_template );
$load_template = $overridden_template;
}
}
// avoid instant explosions
if( !empty( $load_template) ) {
load_template( $load_template );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment