如果您隨插件一起提供前端輸出,那么其他開發(fā)人員可以出于主題目的對其進行修改,這一點很重要。這可以通過過濾器或操作來完成,但我認為最簡單和最常見的方法是提供可以覆蓋的模板路徑。
定義可在主題中使用的路徑和文件命名約定是常見的做法。通過這種方式,WordPress 本身可以讓您修改其模板,而 WooCommerce 等其他大公司也可以這樣做。它簡單直觀。主題開發(fā)人員可以將模板文件復(fù)制粘貼到他們的主題中,然后開始更改輸出。不需要額外的樣板代碼。
尋找路徑
對于我們的意圖來說,最重要的是尋路。有幾個地方可能很有趣。
- 活躍主題
- 父主題
- 其他插件
- 默認模板
此列表還將反映模板查找的優(yōu)先級。
活躍主題
我們可以使用locate_template函數(shù)在活動主題中搜索模板文件。如果沒有找到,它將返回一個路徑字符串或 false。
<?php
namespace PublicFunctionOrg\WordPress;
class Templates {
private $theme_dir = "/my-plugin-templates/";
public function get_template_path($template){
if ( $overridden_template = locate_template( $this->theme_dir.$template ) ) {
return $overridden_template;
}
return false;
}
}
$templates = new Templates();
$filepath = $templates->get_template_path("my-template.php");
父主題
通常,locate_template 函數(shù)還應(yīng)該在父主題目錄中查找模板文件。但我遇到過沒有的情況。這就是為什么我總是為父主題模板文件明確添加第二次查找。
<?php
namespace PublicFunctionOrg\WordPress;
class Templates {
private $theme_dir = "/my-plugin-templates/";
public function get_template_path($template){
$path = $this->theme_dir.$template;
if( is_file( get_template_directory().$path)){
return get_template_directory().$path;
}
return false;
}
}
$templates = new Templates();
$filepath = $templates->get_template_path("my-template.php");
其他插件
如果其他插件可能有興趣覆蓋我們的插件模板,我們首先需要提供一個過濾器來允許插件注冊它們的模板路徑。然后我們將在查找中包含這些路徑。
<?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");
默認模板
最后的查找是最簡單的。只需使用您的插件默認模板。
<?php
namespace PublicFunctionOrg\WordPress;
class Templates {
public function get_template_path($template){
$path = plugin_file_path(__FILE__)."/templates/";
if(is_file("$path/$template")){
return "$path/$template";
}
return false;
}
}
// find templates
$templates = new Templates();
$filepath = $templates->get_template_path("my-template.php");
合并所有代碼
現(xiàn)在我們已經(jīng)為每個可以找到模板的單獨位置找到了解決方案,讓我們組合這些目的地來創(chuàng)建組合模板查找類。
<?php
namespace PublicFunctionOrg\WordPress;
class Templates {
const THEME_DIR = "/my-plugin-templates/";
const FILTER_ADD_TEMPLATES_PATHS = "my_plugin_add_template_paths";
public function get_template_path($template){
if ( $overridden_template = locate_template( self::THEME_DIR.$template ) ) {
return $overridden_template;
}
$path = self::THEME_DIR.$template;
if( is_file( get_template_directory().$path)){
return get_template_directory().$path;
}
$paths = apply_filters(self::FILTER_ADD_TEMPLATES_PATHS, []);
foreach ($paths as $path){
if(is_file("$path/$template")){
return "$path/$template";
}
}
$path = plugin_file_path(__FILE__)."/templates/";
if(is_file("$path/$template")){
return "$path/$template";
}
return false;
}
}
渲染模板
如果我們想在插件中渲染一個模板,我們應(yīng)該首先將默認模板文件templates/my-template.php添加到我們插件的模板目錄中。這將是主副本,因此在環(huán)境文檔中詳細說明是非常有幫助的。
<?php
//... some other plugin stuff
add_filter("the_content", function($content){
$data = new MyModel();
$templates = new PublicFunctionOrg\Wordpress\Templates();
$filepath = $templates->get_template_path("my-template.php");
if($filepath){
ob_start();
include $filepath;
$content.= ob_get_contents();
ob_end_clean();
}
return $content;
});
<?php
/**
* @var SomeClass $this
* @var MyModel $data
*/
echo $data->getDescription();
總結(jié)
這些是我的模板類的起點的基礎(chǔ)知識。您可以在 GitHub 上的wp-components 存儲庫中找到模板類的主副本。它有一些額外的特性和配置方法,但基本上它的工作方式是一樣的。
注:本文來自 medium.com,作者為 Edward Bock,由 WordPress大學(xué) 翻譯整理。
拓展閱讀:
- WordPress 插件開發(fā)教程
- WordPress 插件開發(fā)時在插件列表頁的幾個常用Hook及用例
- WordPress插件開發(fā) 之 添加自動檢測更新和一鍵升級功能
- 根據(jù)另一個插件的激活狀態(tài)停用 WordPress 插件




