Last active
December 30, 2016 11:04
-
-
Save emgk/51f07452babafdf58b9c301f0d5611f8 to your computer and use it in GitHub Desktop.
Templating Wordpress plugin like woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Templating wp plugin like woocommerce | |
* Author: Govind Kumar | |
* Author URI: emgk.github.io | |
* | |
* Description - | |
* If you want to create template structure for your plugin, | |
* that any developer or theme can overide it, Then use this script. | |
* | |
*/ | |
class PluginTemplate { | |
protected static $_instance = NULL; | |
private $templateDir = ''; | |
/** | |
* Create new instance of the class if not exists. | |
* | |
*/ | |
public static function get_instance() { | |
if ( ! isset( self::$_instance ) ) { | |
self::$_instance = new self; | |
} | |
return self::$_instance; | |
} | |
/** | |
* Not accessible from outside. | |
*/ | |
public function __construct( ) { } | |
/** | |
* Set plugin template folder. | |
*/ | |
public function setTempDir( $dirname ){ | |
// return if $dirname not set. | |
if ( ! $dirname ) { | |
$dirname = 'plugintemplate/'; | |
} | |
// Set template name; | |
$this->templateDir = $dirname . '/'; | |
} | |
/** | |
* Get plugin's template folder name. | |
*/ | |
public function getTempDir( ) { | |
// Return plugin template folder name. | |
return $this->templateDir; | |
} | |
/** | |
* Function to call template. | |
* | |
*/ | |
public function get_template_part( $slug, $name = '' ){ | |
$template = ''; | |
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php | |
$template = locate_template( array( "{$slug}-{$name}.php", $this->getTempDir() . "{$slug}-{$name}.php" ) ); | |
// Get default slug-name.php | |
if ( ! $template && $name && file_exists( plugin_dir_path(__FILE__) . "templates/{$slug}-{$name}.php" ) ) { | |
$template = plugin_dir_path(__FILE__) . "templates/{$slug}-{$name}.php"; | |
} | |
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php | |
if ( ! $template ){ | |
$template = locate_template( array( "{$slug}.php", $this->getTempDir() . "{$slug}.php" ) ); | |
} | |
if ( $template ) { | |
load_template( $template, false ); | |
} | |
} | |
} | |
/** | |
* Below part is just demostration for calling the template. | |
* You can remove it or use this in another file where you want to call template. | |
*/ | |
// Register new shortcode on load. | |
add_action('init',function(){ | |
add_shortcode('plugin_template','shortcode_callback'); | |
}); | |
// Shortcode callback function to call template part . | |
function shortcode_callback(){ | |
// Set template directory, my plugin template folder name is 'another'. | |
PluginTemplate::get_instance()->setTempDir('another'); | |
// Call template anywhere. | |
PluginTemplate::get_instance()->get_template_part('content','product'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment