Skip to content

Instantly share code, notes, and snippets.

@ShinichiNishikawa
Last active September 6, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ShinichiNishikawa/25ff4e795f3d00ed5b94 to your computer and use it in GitHub Desktop.
Save ShinichiNishikawa/25ff4e795f3d00ed5b94 to your computer and use it in GitHub Desktop.
Load template file in plugin folder.
<?php
class nskw_template_loader {
public function __construct() {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
}
public static function template_loader( $template ) {
// テンプレートファイルの場所
$template_dir = plugin_dir_path( __DIR__ ) . 'templates/';
if ( is_search() && 'estate' == $_GET['s'] ) {
// 探すべきファイル名
$file_name = 'search-estate.php';
} elseif ( is_singular( 'estate' ) ) {
$file_name = 'single-estate.php';
}
if ( isset( $file_name ) ) {
// テーマ(子 → 親)のファイルを先に探す
$theme_file = locate_template( $file_name );
}
if ( isset( $theme_file ) && $theme_file ) {
$template = $theme_file;
} elseif ( isset( $file_name ) && $file_name ) {
$template = $template_dir . $file_name;
}
return $template;
}
}
new nskw_template_loader();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment