問題描述
我喜歡創建子插件
説明: –
我有一個名為 X 的父插件。這將獨立工作 (像其他現有的插件) 。
現在我決定為該插件 X 添加一些額外的功能 (將其視為升級版本) 。所以我想通過另一個插件 Y 創建額外的功能,這將取決於 (並從中繼承)X. 這個插件 Y 不能使用插件 X 。
我該怎麼做?
最佳解決方案
最好的方法是讓你的 X 插件用自己的鈎子做動作和過濾器,所以新的插件 (在你的情況下 Y) 可以與插件 X 的功能和數據進行交互。定義你自己的鈎子是相當簡單和簡單的。
行動鈎
從抄本:
Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.
一個新的動作鈎子的例子:
Function whatever(){
//regular stuff you do normally
do_action('Name-Of-Your-Action-hook', $args1,$args2)
//regular stuff you do normally
}
現在我們可以與該函數進行交互,並使用它的參數 ($ args1,$ args2) 使用’Name-Of-Your_hook’ 鈎子
add_action('Name-Of-Your-Action-hook','hook_function_callback');
過濾鈎
從抄本:
Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.
一個新的過濾器鈎子的例子:
Function whatever(){
//regular stuff you do normally
$output = apply_filters('Name-Of-Your-Filter-hook', $output,$args1,$args2)
//regular stuff you do normally
}
現在我們可以與該函數交互,使用’Name-Of-Your-Filter-hook’ 鈎子過濾 $ output use 及其參數 ($ args1,$ args2)
add_filter('Name-Of-Your_hook','hook_function_callback');
一個很好的例子是 contact form 7
-
聯繫表格 7 – 活動監視器插件
-
聯繫表格 7 動態文本擴展
-
聯繫表格 7 日曆
-
聯繫表格 7 Textarea Wordcount
-
聯繫表格 7 Customfield 郵寄
-
聯繫表格 7 到數據庫擴展
而且更多的是所有 (大多數) 是基於其鈎子來擴展 Contact Form 7 的功能的插件。
次佳解決方案
在您的父插件中添加一些鈎子,子插件可以附加自己的功能。如果是 if(function_exists('parent-plugin-function'))或 class_exists,則將子插件也包裝起來。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。