WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開(kāi)放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
WordPress:Writing a Plugin
介紹
WordPress 1.2版本之前,如果你想要修改WordPress行為,你必須編輯(或者 "hack") WordPress 源代碼。盡管如此,在更多的現(xiàn)行WordPress版本中 ,你可以通過(guò)使用插件很容易的修改添加核心功能?;镜牟寮Y(jié)構(gòu)的想法是保持WordPress核心相關(guān)內(nèi)容的簡(jiǎn)潔,但是富有靈活性,足以使各個(gè)輸入輸出方面可以通過(guò)插件來(lái)修改。下面是定義:
- WordPress 插件
- WordPress 插件是一個(gè)程序,或者是一組或更多函數(shù),由PHP腳本語(yǔ)言編寫,添加某種特殊的功能或者服務(wù)到WordPress weblog,使用接入點(diǎn)和WordPress Plugin Application Program Interface (API) 提供的方法,可以與weblog很好的結(jié)合在一起。
如果你發(fā)現(xiàn)你希望WordPress能擁有一些新的或者改良的功能,第一件事就是搜索各種插件庫(kù)(你可以從 插件文章中了解),然后看是否某人已經(jīng)創(chuàng)建了這樣的適合你的需要的插件。如果沒(méi)有,本文將指導(dǎo)你如何創(chuàng)建你自己的插件的過(guò)程。
本文假設(shè)你已經(jīng)熟悉基本的WordPress功能以及 PHP編程。
資源
- 有很多插件開(kāi)發(fā)者的的全面的文章列表和資源,插件資源中包括有關(guān)編寫插件的外部文章以及關(guān)于特別話題的文章。
- 另外一個(gè)了解有關(guān)插件的好方法是看寫的好的插件的PHP 源代碼,如 Hello Dolly, 一個(gè)分布式插件。
- 一旦你寫了插件,閱讀插件提交和宣傳來(lái)學(xué)習(xí)如何讓它更廣泛的散布開(kāi)來(lái)。
創(chuàng)建插件
本部分進(jìn)入你需要遵守的幾個(gè)步驟,和一些考慮什么時(shí)候建立一個(gè)結(jié)構(gòu)良好的 WordPress插件。
名字,文件和位置
插件名字
創(chuàng)建插件的第一個(gè)任務(wù)是想出插件要做什么,然后起一個(gè)(希望是唯一的)名字。 查看插件 和其他的相關(guān)庫(kù),驗(yàn)證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開(kāi)發(fā)者選擇使用能描述插件功能的那個(gè)名字; 如,一個(gè)聯(lián)系到天氣的插件可能含有單詞"weather"在名字中。名字可以是好幾個(gè)詞。
插件文件
下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來(lái)。如,如果你的插件叫做"Fabulous Functionality",你可以把你的PHP文件起名為 fabfunc.php。試著取一個(gè)唯一的名字。人們安裝你的插件的時(shí)候會(huì)把這個(gè)PHP文件放到WordPress插件目錄中,wp-content/plugins/,所以沒(méi)有兩個(gè)不同的在使用的插件擁有相同的PHP文件名。
另外一個(gè)操作是把你的插件分成幾個(gè)文件,你的插件至少要有一個(gè)PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語(yǔ)言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如fabfunc 和 fabfunc.php,就把所有的插件文件放到這個(gè)目錄下,然后告訴你的插件用戶安裝整個(gè)目錄到wp-content/plugins/下。
在這個(gè)文章的其余部分,"插件PHP文件"引用了主插件PHP文件,可以在 wp-content/plugins/ 或者一個(gè)子目錄下
自述文件
如果你想在http://wordpress.org/extend/plugins/上建立一個(gè)插件主機(jī),你還需要用標(biāo)準(zhǔn)格式創(chuàng)建readme.txt文件,把它放到你的插件中去。參見(jiàn) http://wordpress.org/extend/plugins/about/readme.txt 獲得這格式的描述.
主頁(yè)
創(chuàng)建一個(gè)網(wǎng)頁(yè)來(lái)作為你的插件的主頁(yè)也是非常有用的。這個(gè)頁(yè)面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個(gè)版本,版本更替的時(shí)候你的插件做出了哪些改動(dòng),如何使用插件等等。
文件標(biāo)題
現(xiàn)在開(kāi)始在你的插件的主PHP文件中添加信息。
標(biāo)準(zhǔn)插件信息
插件主PHP文件的頂部必須包含一個(gè)標(biāo)準(zhǔn)插件信息標(biāo)題。這個(gè)標(biāo)題讓W(xué)ordPress 識(shí)別你的插件存在,添加到插件管理界面以便激活,載入,運(yùn)行; 沒(méi)有標(biāo)題,你的插件將不會(huì)被激活也不會(huì)運(yùn)行,如下是標(biāo)題格式:
<?php /* Plugin Name: Name Of The Plugin Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates Description: A brief description of the plugin. Version: The plugin's Version Number, e.g.: 1.0 Author: Name Of The Plugin Author Author URI: http://URI_Of_The_Plugin_Author */ ?>
WordPress需要用來(lái)識(shí)別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來(lái)創(chuàng)建插件管理界面上的插件表格。這些行的順序并不重要。
許可
根據(jù)帶有許可信息這個(gè)標(biāo)準(zhǔn)標(biāo)題是個(gè)慣例。多數(shù)插件使用WordPress 使用的GPL 許可,或者是一個(gè) compatible with the GPL的許可。簡(jiǎn)要說(shuō)明下GPL許可,在你的插件中添加如下幾行:
<?php /* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?>
規(guī)劃你的插件
現(xiàn)在是該讓你的插件做些什么的時(shí)候了。這個(gè)部分包含了一些一般的關(guān)于插件開(kāi)發(fā)的思想,描述了如何完成你的插件需要完成的幾個(gè)任務(wù)。
WordPress 插件 Hooks
很多插件通過(guò)連接一個(gè)或者多個(gè)的WordPress 插件 "hooks"來(lái)完成它們的目標(biāo)。 插件hooks的工作方式是在WordPress運(yùn)行的時(shí)候,在各個(gè)不同的時(shí)候,WordPress檢查是否有插件在那時(shí)有注冊(cè)過(guò)的函數(shù)在此刻運(yùn)行,如果有的話,函數(shù)運(yùn)行。三個(gè)函數(shù)更改WordPress的默認(rèn)行為。
例如,在WordPress添加文章標(biāo)題到瀏覽器輸出的時(shí)候,首先檢查是否有任何插件為"filter"hook注冊(cè)了一個(gè)函數(shù)叫做"the_title"。如果有,標(biāo)題文字會(huì)被每個(gè)注冊(cè)函數(shù)按順序傳遞,最終結(jié)果就是打印出來(lái)的結(jié)果。這樣,如果你的插件需要添加一些信息到打印標(biāo)題,它可以注冊(cè)一個(gè)"the_title" filter函數(shù).
另一個(gè)例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁(yè)面的底部,它檢查看是否某個(gè)插件已經(jīng)為 "wp_footer" action hook注冊(cè)函數(shù),然后依次運(yùn)行。
你可以了解更多關(guān)于如何為filter 和 action hooks注冊(cè)函數(shù),什么插件hooks在WordPress和在插件API中可用。如果你在WordPress代碼中發(fā)現(xiàn)一個(gè)你想使用action或者filter的地方,但是WordPress沒(méi)有,你可以建議使用新的hooks (建議通常都會(huì)被采納); 如何建議參見(jiàn)提交錯(cuò)誤。
模板標(biāo)簽
另外一種用插件添加WordPress功能的方法是通過(guò)建立自定義 模板標(biāo)簽。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個(gè)叫做geotag_list_states()的模板標(biāo)簽函數(shù),列出所有的有關(guān)國(guó)家,并帶有國(guó)家歸檔頁(yè)面的鏈接。
想要定義一個(gè)模板標(biāo)簽,需要簡(jiǎn)單的寫一個(gè)PHP 函數(shù)并為插件用戶在插件主頁(yè)或者是插件PHP主文件中寫下資料說(shuō)明。在給函數(shù)做資料說(shuō)明時(shí),給出一個(gè)具體需要加入什么到主題文件中以使用函數(shù)的例子是個(gè)不錯(cuò)的想法包括 <?php 和 ?>.
保持插件數(shù)據(jù)到數(shù)據(jù)庫(kù)
大多數(shù)插件需要從站長(zhǎng)或者blog用戶得到一些信息并保存在sessions中,為了在它的filter 函數(shù), action 函數(shù)和模板函數(shù)中使用。這個(gè)信息必須保存在WordPress 數(shù)據(jù)庫(kù)中,為了在sessions中可以持久穩(wěn)定。有兩種基本方法可以保存插件數(shù)據(jù)到數(shù)據(jù)庫(kù)中:
- 使用WordPress "操作"機(jī)制(下有描述)。這個(gè)方法適合儲(chǔ)存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時(shí)站長(zhǎng)輸入的,今后幾乎不會(huì)更改的數(shù)據(jù)。
- 創(chuàng)建一個(gè)新的自定義的數(shù)據(jù)庫(kù)表格。這種方法適合聯(lián)合單獨(dú)的文章,頁(yè)面,附件或者評(píng)論的數(shù)據(jù)—一種隨著時(shí)間而增加的,沒(méi)有單獨(dú)的名字的數(shù)據(jù)。參見(jiàn) 創(chuàng)建插件表格.
WordPress 操作機(jī)制
參見(jiàn)創(chuàng)建操作頁(yè)面 得到關(guān)于如何創(chuàng)建自動(dòng)為你保存操作的頁(yè)面的信息。
WordPress有一個(gè)保存,更新,重新得到個(gè)體, 制定的數(shù)據(jù)("操作")機(jī)制,在 WordPress 數(shù)據(jù)庫(kù)中。操作值可以是字符串,數(shù)組或者是PHP對(duì)象(它們將被 "連載",或者在存儲(chǔ)前轉(zhuǎn)變?yōu)樽址?,在重新得到個(gè)體時(shí)反連載)。操作名字是字符串,它們必須是唯一的,這樣它們不會(huì)與其他 WordPress或者是插件起沖突。
這里是你的插件可以用來(lái)進(jìn)入WordPress操作的主要函數(shù)。
add_option($name, $value, $description, $autoload);
- Creates a new option; does nothing if option already exists.
- $name
- Required (string). Name of the option to be added.
- $value
- Optional (string), defaults to empty string. The option value to be stored.
- $description
- Optional (string), defaults to empty string. Description of the option, which is placed into the WordPress database in case someone browses the database to see what options are there.
- $autoload
- Optional, defaults to 'yes' (enum: 'yes' or 'no'). If set to 'yes' the setting is automatically retrieved by the get_alloptions function.
get_option($option);
- Retrieves an option value from the database.
- $option
- Required (string). Name of the option whose value you want returned. You can find a list of the default options that are installed with WordPress at the WordPress:Option Reference.
update_option($option_name, $newvalue);
- Updates or creates an option value in the database (note that add_option does not have to be called if you do not want to use the $description or $autoload parameters).
- $option_name
- Required (string). Name of the option to update.
- $newvalue
- Required. The new value for the option.
Administration Panels
Assuming that your plugin has some options stored in the WordPress database (see section above), you will probably want it to have an administration panel that will enable your plugin users to view and edit option values. The methods for doing this are described in WordPress:Adding Administration Menus.
管理面板
假設(shè)你的插件有一些操作存儲(chǔ)在WordPress數(shù)據(jù)庫(kù)中 (參見(jiàn)上面的部分),你也許希望有一個(gè)管理面板,允許你的插件用戶來(lái)查看和編輯操作。方法可以在 添加管理目錄中找到。
Internationalizing Your Plugin
Once you have the programming for your plugin done, another consideration (assuming you are planning on distributing your plugin) is internationalization. Internationalization is the process of setting up software so that it can be localized; localization is the process of translating text displayed by the software into different languages. WordPress is used all around the world, so it has internationalization and localization built into its structure, including localization of plugins. For background on WordPress's use of GNU gettext for localization, see WordPress:Translating WordPress.
讓你的插件國(guó)際化
一旦你插件的程序完成了,另一個(gè)需要考慮的是(假設(shè)你打算發(fā)布你的插件) 國(guó)際化. 國(guó)際化是建立軟件的一個(gè)過(guò)程,這樣它才可以本地化 ;本地化是翻譯軟件的顯示文本為其他語(yǔ)言。WordPress是全世界使用的,所以它有國(guó)際化和本地化版本,包括本地化的插件。想要知道關(guān)于WordPress的 GNU gettext的本地化使用,參見(jiàn)WordPress:Translating WordPress.
It is highly recommended that you internationalize your plugin, so that users from different countries can localize it. The process is fairly straightforward:
- Choose a translation "text domain" name for your plugin. This is generally the same as your plugin file name (without the .php), and must be unique among plugins the user has installed.
- Wherever your plugin uses literal text strings that will be displayed to the user (known as "messages"), wrap them in one of the two WordPress gettext functions. Note that in a plugin, you need to use the second argument, passing in the translation text domain name you chose, unlike in the core of WordPress (which leaves the $domain argument blank).
- __($message, $domain)
- Translates $message using the current locale for $domain. Wrap text strings that you are going to use in calculations with this function.
- _e($message, $domain)
- Translates $message using the current locale for $domain, and then prints it on the screen. Wrap text strings that you are directly printed with this function.
強(qiáng)力推薦你國(guó)際化你的插件,這樣來(lái)自不同國(guó)家的用戶,可以本地化插件,過(guò)程如下:
- 給插件選擇一個(gè)翻譯"文本區(qū)域"名字。這通常和你的插件文件名字一樣(除了 .php), 而且必須是用戶安裝過(guò)的插件中唯一的。
- 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個(gè)或者兩個(gè) WordPress gettext 函數(shù)把它們包圍起來(lái)。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 $domain 這部分空白).
- __($message, $domain)
- 使用現(xiàn)有本地$domain翻譯 $message。換行你將要用來(lái)用這個(gè)函數(shù)計(jì)算的文字字符串。
- _e($message, $domain)
- 使用現(xiàn)有本地$domain翻譯 $message, 然后在屏幕上顯示出來(lái),換行你使用這個(gè)函數(shù)直接顯示出的文本字符串。
- Create a POT file (translation catalog listing all the translatable messages) for your plugin, and distribute it with your plugin. Users will need to put their translated MO file in the same directory as your plugin's PHP file, and name it domain-ll_CC.mo, where ll_CC is the name of their locale. See WordPress:Translating WordPress for information on POT files, MO files, and locales.
- Load the translations for the current locale and your text domain by calling load_plugin_textdomain before either of the gettext functions is called, but as late as possible in the session (because some multi-lingual plugins change the locale when they load). One possible implementation is to define an initialization function that is called at the top of all of your plugin functions. For instance, assuming your text domain is "fabfunc":
$fabfunc_domain = 'fabfunc'; $fabfunc_is_setup = 0; function fabfunc_setup() { global $fabfunc_domain, $fabfunc_is_setup; if($fabfunc_is_setup) { return; } load_plugin_textdomain($fabfunc_domain, 'wp-content/plugins'); }
If your plugin is in its own subdirectory, append that to the second argument of load_plugin_textdomain.
- 創(chuàng)建一個(gè)POT文件(翻譯目錄列出了所有的可以翻譯的信息),隨著你的插件分發(fā)出去。用戶將需要把他們的翻譯過(guò)的MO文件放在你的插件的PHP文件的同一目錄下,命名為domain-ll_CC.mo,那里ll_CC是本地文件的名字。參見(jiàn)翻譯WordPress以獲得關(guān)于POT文件,MO文件和本地文件的信息。
- 通過(guò)在調(diào)用gettext函數(shù)之前調(diào)用load_plugin_textdomain為現(xiàn)有本地文件和你的文本區(qū)域文件載入翻譯,在session中越晚越好,(因?yàn)橐恍┒嗾Z(yǔ)言插件載入時(shí)改變了本地文件)。一個(gè)可能的辦法是定義一個(gè)初始化函數(shù),在所有插件函數(shù)之前調(diào)用,如,假設(shè)你的文本區(qū)域是"fabfunc":
$fabfunc_domain = 'fabfunc'; $fabfunc_is_setup = 0; function fabfunc_setup() { global $fabfunc_domain, $fabfunc_is_setup; if($fabfunc_is_setup) { return; } load_plugin_textdomain($fabfunc_domain, 'wp-content/plugins'); }
If your plugin is in its own subdirectory, append that to the second argument of load_plugin_textdomain
If you are reading this section because you want to internationalize a Theme, you can basically follow the steps above, except:
- The MO file goes into the theme directory (same place as style.css).
- The MO file is named ll_CC.mo, where ll_CC is the name of the locale (i.e. the domain is NOT part of the file name).
- To load the text domain, put the following (inside a PHP escape if necessary) in your theme's functions.php file:
load_theme_textdomain('your_domain');
如果你閱讀了這個(gè)部分因?yàn)槟阆胍獓?guó)際化一個(gè)主題,你大體上可以按照上面的步驟來(lái),除非:
- MO文件在主題目錄中(與style.css同樣的位置).
- MO 文件命名為ll_CC.mo, ll_CC是本地文件名(這個(gè)域不是文件名字的一部分).
- 想要載入文本域,加入如下代碼(在某個(gè)PHP 溢出,如果需要的話) 到你的主題的functions.php文件:
load_theme_textdomain('your_domain');
Plugin Development Suggestions
This last section contains some random suggestions regarding plugin development.
- The code of a plugin should follow the WordPress:WordPress Coding Standards. Please consider the WordPress:Inline Documentation Standards as well.
- All the functions in your plugin need to have unique names that are different from functions in the WordPress core, other plugins, and themes. For that reason, it is a good idea to use a unique function name prefix on all of your plugin's functions. Another possibility is to define your plugin functions inside a class (which also needs to have a unique name).
- Do not hardcode the WordPress database table prefix (usually "wp_") into your plugins. Be sure to use the $wpdb->prefix variable instead.
- Database reading is cheap, but writing is expensive. Databases are exceptionally good at fetching data and giving it to you, and these operations are (usually) lightning quick. Making changes to the database, though, is a more complex process, and computationally more expensive. As a result, try to minimize the amount of writing you do to the database. Get everything prepared in your code first, so that you can make only those write operations that you need.
- SELECT only what you need. Even though databases fetch data blindingly fast, you should still try to reduce the load on the database by only selecting that data which you need to use. If you need to count the number of rows in a table don't SELECT * FROM, because all the data in all the rows will be pulled, wasting memory. Likewise, if you only need the post_id and the post_author in your plugin, then just SELECT those specific fields, to minimize database load. Remember: hundreds of other processes may be hitting the database at the same time. The database and server each have only so many resources to spread around amongst all those processes. Learning how to minimize your plugin's hit against the database will ensure that your plugin isn't the one that is blamed for abuse of resources.
插件開(kāi)發(fā)建議
最后一部分包括一些關(guān)于插件開(kāi)發(fā)的建議。
- 插件代碼應(yīng)該遵循 WordPress編碼標(biāo)準(zhǔn)。請(qǐng)參照內(nèi)嵌文檔標(biāo)準(zhǔn)。
- 所有的插件中的函數(shù)需要有唯一的名字,因?yàn)檫@個(gè)原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個(gè)很好的主意。另一個(gè)可能性是在類中定義你的插件函數(shù)(也需要有唯一名字).
- 不要吧WordPress數(shù)據(jù)庫(kù)表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的$wpdb->前綴來(lái)代替。
- 數(shù)據(jù)庫(kù)閱讀很簡(jiǎn)單但是寫確很難,數(shù)據(jù)庫(kù)是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫(kù)中做改動(dòng),卻是一個(gè)復(fù)雜的多的過(guò)程,很難。所以試著最小化寫入的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫操作。
- 只選擇你需要的。盡管數(shù)據(jù)庫(kù)取數(shù)據(jù)非??欤阈枰囍鴾p少數(shù)據(jù)庫(kù)的負(fù)載,通過(guò)只選擇你需要的數(shù)據(jù)。如果你需要知道一個(gè)表格有多少行,不要SELECT * FROM,因?yàn)樗行兄械乃袛?shù)據(jù)都會(huì)被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要SELECT那些特定的區(qū)域,來(lái)減小數(shù)據(jù)庫(kù)負(fù)載。記住: 大量的其他進(jìn)程可能同時(shí)需要使用數(shù)據(jù)庫(kù)。數(shù)據(jù)庫(kù)和服務(wù)器每個(gè)都有大量的資源供這些進(jìn)程使用。了解如何減小你的插件需要的資源會(huì)保證你的插件不會(huì)是被人說(shuō)成濫用資源的哪種。
External Resources
- Simplified AJAX For WordPress Plugin Developers using Jquery(10APR08)
- (10MAR08)
- 12 part "How to Write a Wordpress Plugin" at DevLounge.net by Ronald Huereca (PDF)
- How to create WordPress Plugin from a scratch (9AUG07)
- Using AJAX with your WordPress Plugin, also at DevLounce.net (25MAY07)
- (22FEB05)
外部資源
- 為WordPress插件開(kāi)發(fā)人員使用的Jquery的簡(jiǎn)單化AJAX(10APR08)
- (10MAR08)
- 12 part "如何寫Wordpress 插件" at DevLounge.net ,來(lái)自 Ronald Huereca (PDF)
- 如何創(chuàng)建WordPress插件 (9AUG07)
- your WordPress 插件中使用AJAX, DevLounce.net上也有 (25MAY07)
- (22FEB05)