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.在本质上的可重用的自定义功能决定的理想地点的自定义代码。