Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active July 22, 2021 20:19
Show Gist options
  • Save EdwardBock/250716fecf5b913f0909e14594bc02dd to your computer and use it in GitHub Desktop.
Save EdwardBock/250716fecf5b913f0909e14594bc02dd to your computer and use it in GitHub Desktop.
Provide template paths by filter
<?php
add_filter("my_plugin_add_template_paths", function($paths){
$paths[] = plugin_file_path(__FILE__)."/my-templates-dir/";
return $paths;
});
<?php
namespace PublicFunctionOrg\WordPress;
class Templates {
public function get_template_path($template){
$paths = apply_filters("my_plugin_add_template_paths", []);
foreach ($paths as $path){
if(is_file("$path/$template")){
return "$path/$template";
}
}
return false;
}
}
// find templates
$templates = new Templates();
$filepath = $templates->get_template_path("my-template.php");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment