Skip to content

Instantly share code, notes, and snippets.

@goldhat
Last active January 4, 2017 17:49
Show Gist options
  • Save goldhat/e12073f4fed53b311cf87fe5c1059b40 to your computer and use it in GitHub Desktop.
Save goldhat/e12073f4fed53b311cf87fe5c1059b40 to your computer and use it in GitHub Desktop.
Find template functions for WP plugins
/**
* 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.
*/
function quizmaster_get_template( $template_name, $args = array(), $tempate_path = '', $default_path = '' ) {
if ( is_array( $args ) && isset( $args ) ) :
extract( $args );
endif;
$template_file = quizmaster_locate_template( $template_name, $tempate_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/quizmaster/$template_name
* 2. /themes/theme/$template_name
* 3. /plugins/quizmaster/templates/$template_name.
*
* @since 1.0.0
*
* @param string $template_name Template to load.
* @param string $string $template_path Path to templates.
* @param string $default_path Default path to template files.
* @return string Path to the template file.
*/
function quizmaster_locate_template( $template_name, $template_path = '', $default_path = '' ) {
// Set variable to search in woocommerce-plugin-templates folder of theme.
if ( ! $template_path ) :
$template_path = 'quizmaster/';
endif;
// Set default plugin templates path.
if ( ! $default_path ) :
$default_path = plugin_dir_path( __FILE__ ) . '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( 'quizmaster_locate_template', $template, $template_name, $template_path, $default_path );
}
@goldhat
Copy link
Author

goldhat commented Jan 4, 2017

Why isn't syntax highlighting working?

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