Skip to content

Instantly share code, notes, and snippets.

@goldhat
Created September 16, 2017 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldhat/89bdaaba90fc6d81c4f91b3f26ba6ed3 to your computer and use it in GitHub Desktop.
Save goldhat/89bdaaba90fc6d81c4f91b3f26ba6ed3 to your computer and use it in GitHub Desktop.
Override templates support for themes provided in WordPress plugin
<?php
class Clay_Template {
public $override_dir;
public $hook_prefix;
public function __construct( $override_dir, $hook_prefix ) {
$this->override_dir = $override_dir;
$this->hook_prefix = $hook_prefix;
}
/**
* Get template.
*
* Search for the template and include the file.
*
* @since 1.0.0
*
* @see wcpt_locate_template()
*
* @param string $template_name Template to load.
* @param array $args Args passed for the template file.
* @param string $string $template_path Path to templates.
* @param string $default_path Default path to template files.
*/
public function parse_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
ob_start();
$this->get_template( $template_name, $args, $template_path, $default_path );
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
/**
* Get template.
*
* Search for the template and include the file.
*
* @since 1.0.0
*
* @see wcpt_locate_template()
*
* @param string $template_name Template to load.
* @param array $args Args passed for the template file.
* @param string $string $template_path Path to templates.
* @param string $default_path Default path to template files.
*/
public function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
if ( is_array( $args ) && isset( $args )) {
extract( $args );
}
$template_file = $this->locate_template( $template_name . '.php', $template_path, $default_path );
if ( ! file_exists( $template_file ) ) :
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template_file ), '1.0.0' );
return;
endif;
include $template_file;
}
/**
* Locate template.
*
* Locate the called template.
* Search Order:
* 1. /themes/theme/$override_dir/$template_name
* 2. /themes/theme/$template_name
* 3. /plugins/[gravity-perk]/templates/$template_name.
*
* @since 1.0.0
*
* @param string $template_name Template to load.
* @param string $template_path Path to templates.
* @param string $default_path Default path to template files.
* @return string Path to the template file.
*/
public function locate_template( $template_name, $template_path = '', $default_path = '' ) {
// Set variable to search in override directory of theme.
if ( ! $template_path ) {
$template_path = $this->override_dir . '/';
} else {
$template_path = $template_path . '/';
}
// Set default plugin templates path.
if ( ! $default_path ) :
$dir = plugin_dir_path( __FILE__ );
$dir = str_replace( 'src/', '', $dir );
$default_path = $dir . 'templates/'; // Path to the template folder
endif;
// Search template file in theme folder.
$template = locate_template( array(
$template_path . $template_name,
$template_name
) );
// Get plugins template file.
if ( ! $template ) :
$template = $default_path . $template_name;
endif;
return apply_filters( $this->hook_prefix . '_locate_template', $template, $template_name, $template_path, $default_path );
}
}
@goldhat
Copy link
Author

goldhat commented Sep 16, 2017

This class can be used in any plugin development project for WordPress. It is currently used in QuizMaster, and Gravity Perks as well numerous other plugin projects. Make sure to rename the class for your project so it doesn't conflict with other plugins that may be using it, for instance if your plugin is named "Cats" rename to "Cats_Template". This template loading system will check both the WordPress active theme and child themes, looking for the prefix you specify when loading the class. For instance if the override directory is "cats", and the active theme is "avada" it will look for override templates at /avada/cats/. Templates found in the theme should maintain any directory structure found in the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment