Recently the question (How to add add custom functions to a theme) was posed in最近的問題(如何添加添加自定義功能,以一個主題)中提出的 WordPress Support Forum在WordPress支持論壇 . Personally I faced the same question when re-designing我個人面臨著同樣的問題時,重新設計 this site本網站 . The following is a discussion of the option with the pros and cons.以下是討論的選項與優點和缺點。

There are two solutions.有兩個解決辦法。
The first is to create a plugin which goes with the theme.首先是創建一個插件,其中有雲與主題。 However the theme, when invoking functions defined in the plugin should check first for the availability of the function.不過主題時,引用所界定的職能的插件應該檢查第一為可用的功能。 It should also provide a fallback option if the plugin is not available or has not been activated.它還應提供後備選項,如果插件是無法使用或尚未啟用。

 if(function_exists('your_function_name')) {     // Invoke your function: your_function_name()...如果( function_exists ( ' your_function_name ' ) ) ( / /引用您的功能: your_function_name ()... else {     // Execute fallback option }否則, ( / /執行後備選項) 

The advantages are:優勢是:

  • The plugin can be independently managed.插件,可以獨立管理。
  • The plugin can be reused for other purposes.該插件可以再用作其他用途。

The disadvantages are:弊端是:

  • It requires another additional step for the theme user to remember.它需要額外的步驟,另一個為主題,用戶要記住。
  • It slightly complicates development and testing.它稍複雜的開發和測試。

The second solution to this problem would be to incorporate the functionaility in a php file (as usual) which resides in the theme directory.第二要解決這個問題將納入functionaility在PHP文件(如常) ,其中居住在主題目錄。 This file is included in header.php like:此文件是包含在header.php ,例如:
include (’your_php_file.php’); 包括( ' your_php_file.php ' ) ;

Yes you may also require it for simplicity like:是您可能還需要它簡單,例如:
require (’your_php_file.php’); 要求( ' your_php_file.php ' ) ;

The advantage to this approach is simplicity of usage and deployment by end-users.的優勢,這種做法是簡單的使用情況和部署的最終用戶。 It also simplifies development.它也簡化了發展。

If the user later decides to switch to a different theme and yet wants to retain the functionality, he would have to re-purpose the custom code into a plugin.如果用戶後來決定將切換到不同的主題,但要保留的功能,他將不得不重新目的的自定義代碼成為一個插件。

In essence the reusability of the custom functionality determines the ideal location of the custom code.在本質上的可重用的自定義功能決定的理想地點的自定義代碼。