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我會收集他們所有一起,如果我-駭客的文件,直到我;米完成,然後訂定有關轉換到自定義插件為你建議,再次感謝