Skip to content

Instantly share code, notes, and snippets.

@bmarshall511
Last active June 29, 2023 10:23
Show Gist options
  • Save bmarshall511/1491d1eaa6245c13a9edc0080e838aba to your computer and use it in GitHub Desktop.
Save bmarshall511/1491d1eaa6245c13a9edc0080e838aba to your computer and use it in GitHub Desktop.
Locate a WordPress plugin template.
<?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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment