Skip to content

Instantly share code, notes, and snippets.

@bmarshall511
Created April 4, 2017 19:16
Show Gist options
  • Save bmarshall511/8e00dd4acd081c3bd1f27bae22122e34 to your computer and use it in GitHub Desktop.
Save bmarshall511/8e00dd4acd081c3bd1f27bae22122e34 to your computer and use it in GitHub Desktop.
Adding WordPress plugin template files.
<?php
/**
* Locate template.
*
* Locate the called template.
* Search Order:
* 1. /themes/theme/templates/$template_name
* 2. /themes/theme/$template_name
* 3. /plugins/plugin/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 PLUGIN_locate_template( $template_name, $template_path = '', $default_path = '' ) {
// Set variable to search in the templates folder of theme.
if ( ! $template_path ) :
$template_path = 'templates/';
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( 'PLUGIN_locate_template', $template, $template_name, $template_path, $default_path );
}
/**
* Get template.
*
* Search for the template and include the file.
*
* @since 1.0.0
*
* @see PLUGIN_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 PLUGIN_get_template( $template_name, $args = array(), $tempate_path = '', $default_path = '' ) {
if ( is_array( $args ) && isset( $args ) ) :
extract( $args );
endif;
$template_file = contests_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;
}
/**
* Template loader.
*
* The template loader will check if WP is loading a template
* for a specific Post Type and will try to load the template
* from out 'templates' directory.
*
* @since 1.0.0
*
* @param string $template Template file that is being loaded.
* @return string Template file that should be loaded.
*/
function PLUGIN_template_loader( $template ) {
$find = array();
$file = '';
if( is_singular() ):
$file = 'single-plugin.php';
elseif( is_tax() ):
$file = 'archive-plugin.php';
endif;
if ( file_exists( PLUGIN_locate_template( $file ) ) ) :
$template = PLUGIN_locate_template( $file );
endif;
return $template;
}
add_filter( 'template_include', 'PLUGIN_template_loader' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment