Skip to content

Instantly share code, notes, and snippets.

@mikeott
Last active March 14, 2023 23:16
Show Gist options
  • Save mikeott/d249c4e61e7026d7265b1c765329f8ee to your computer and use it in GitHub Desktop.
Save mikeott/d249c4e61e7026d7265b1c765329f8ee to your computer and use it in GitHub Desktop.
WordPress: Check if a custom template exists in the theme folder, if not, then use the plugin template file
<?php
// The template
$template = 'my-custom-script.php';
// If a template file exists in the theme folder, use it.
if ( $theme_file = locate_template( array( 'my-custom-files/' . $template ) ) ) {
include(get_template_directory() . '/my-custom-files/' . $template);
} else { // Otherwise, use the plugin template file.
include(plugin_dir_path( __FILE__ ) . '/templates/' . $template);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment