How to add custom functions to a WordPress 1.5 theme?如何添加自定义功能一的WordPress 1.5的主题呢?
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.在本质上的可重用的自定义功能决定的理想地点的自定义代码。
Filed under提起下 CMS Software CMS软件 , , Headline News头条新闻 , , How To如何 , , Pro Blogging赞成Blogging , , Technology技术 , , Web网页 , , WordPress在WordPress | |
| |
RSS 2.0 2.0 | |
Email this Article电子邮件此文章
You may also like to read您也可以想读 |





June 3rd, 2005 at 2:48 pm 2005年6月3日在下午2时48分
Thanks for this.感谢。 ~I am new to both WordPress and PHP but also wanted a few home cooked functions. 〜我新双方的WordPress和PHP ,但也希望数家煮熟的职能。 In my case I wanted to change a couple of WP functions to return differently so I copied these, renamed them and made my changes so as not to disturb distributable files.在我的情况我想改变一对夫妇的可湿性粉剂职能,返回不同的,所以我复制这些,改名为他们作出了我的变化,以便不打扰分派档案。 Not knowing where to put them, I just created a my-hacks.php file and turned that one.不知道在哪里把他们,我只是创造了我- hacks.php档案,把其中一个。 All works fine.所有工程的罚款。 But is this NOT a good place to put my code?但是,这不是一个很好的地方,把我的代码?
Appreciate any advice.欣赏任何意见。 Thanks谢谢
June 3rd, 2005 at 7:54 pm 2005年6月3日在下午7点54分
As I mentioned above, I think you should just create a plugin and put them there.正如我上文所述,我认为你应该只需要建立一个插件,把他们有。
Ideally in your template you can check if the function is available (or whether the plugin is active) and then use it, if not use the default WordPress function.理想在您的模板,您可以检查,如果功能是可用的(或是否插件是积极的) ,然后使用它,如果不使用默认的WordPress的功能。
I too use several such customized functions on my site all packed away in a generic plugin named Impact (named after my custom theme).我也使用几个这样的定制功能,这些功能在我的网站上的所有包装以外的一个通用的插件命名的影响(命名后,我的自定义主题) 。
June 4th, 2005 at 5:45 pm 2005年6月4日下午5时45分
Thanks for the advice.感谢您的意见。 I’ll collect them all together if a my-hacks file until I;m complete then set about converting to a custom plugin as you suggest,Thanks again我会收集他们所有一起,如果我-骇客的文件,直到我;米完成,然后订定有关转换到自定义插件为你建议,再次感谢