問題描述

我喜歡建立子外掛

說明: –

我有一個名為 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。