WIKI使用導航
站長百科導航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機
- cPanel
- 網(wǎng)址導航專題
- 云計算
- 微博營銷
- 虛擬主機管理系統(tǒng)
- 開放平臺
- WIKI程序與應用
- 美國十大主機
WordPress: Writing a Plugin:修訂間差異
無編輯摘要 |
|||
第125行: | 第125行: | ||
add_option($name, $value, $description, $autoload); | add_option($name, $value, $description, $autoload); | ||
</pre> | </pre> | ||
: | : 創(chuàng)建一個新選項;如果選項存在就什么都不做 | ||
;$name: Required (string). | ;$name: Required (string). 將被添加的選項名字 | ||
;$value: Optional (string), | ;$value: Optional (string), 默認為空的字符串。 將被存儲的選項值 | ||
;$description: Optional (string), | ;$description: Optional (string), 默認為空的字符串。選項的描述,被放在wordpress數(shù)據(jù)庫中以防某些人瀏覽數(shù)據(jù)庫去看選項在哪。 | ||
;$autoload: | ;$autoload: 選項,默認為'yes' (enum: 'yes' or 'no').如果設置為'yes',那么設置被<tt>get_alloptions</tt>函數(shù)自動檢索。 | ||
? | |||
<pre> | <pre> | ||
get_option($option); | get_option($option); | ||
</pre> | </pre> | ||
: | : 從數(shù)據(jù)庫中檢索一個選項值 | ||
;$option: Required (string).? | ;$option: Required (string).? 你想要返回的選項值的選項的名稱。你能夠找到被安裝在[[WordPress:Option Reference|相關選項]]中的一列默認選項 | ||
? | |||
<pre> | <pre> | ||
update_option($option_name, $newvalue); | update_option($option_name, $newvalue); | ||
</pre> | </pre> | ||
: | : 在數(shù)據(jù)庫里更新或者創(chuàng)建一個選項值(注:如果你不想使用<tt>$description</tt> 或者 <tt>$autoload</tt>參數(shù),<tt>add_option</tt>就沒必要被訪問。) | ||
;$option_name: Required (string). | ;$option_name: Required (string). 選項名稱更新。 | ||
;$newvalue: Required.? | ;$newvalue: Required.? 為選項賦予新的值。 | ||
=== Administration Panels === | === Administration Panels === |
2008年5月14日 (三) 12:00的版本
介紹
WordPress 1.2版本之前,如果你想要修改WordPress行為,你必須編輯(或者 "hack") WordPress 源代碼。盡管如此,在更多的現(xiàn)行WordPress版本中 ,你可以通過使用插件很容易的修改添加核心功能。基本的插件結構的想法是保持WordPress核心相關內容的簡潔,但是富有靈活性,足以使各個輸入輸出方面可以通過插件來修改。下面是定義:
- WordPress 插件
- WordPress 插件是一個程序,或者是一組或更多函數(shù),由PHP腳本語言編寫,添加某種特殊的功能或者服務到WordPress weblog,使用接入點和WordPress Plugin Application Program Interface (API) 提供的方法,可以與weblog很好的結合在一起。
如果你發(fā)現(xiàn)你希望WordPress能擁有一些新的或者改良的功能,第一件事就是搜索各種插件庫(你可以從 插件文章中了解),然后看是否某人已經(jīng)創(chuàng)建了這樣的適合你的需要的插件。如果沒有,本文將指導你如何創(chuàng)建你自己的插件的過程。
本文假設你已經(jīng)熟悉基本的WordPress功能以及 PHP編程。
資源
- 有很多插件開發(fā)者的的全面的文章列表和資源,插件資源中包括有關編寫插件的外部文章以及關于特別話題的文章。
- 另外一個了解有關插件的好方法是看寫的好的插件的PHP 源代碼,如 Hello Dolly, 一個分布式插件。
- 一旦你寫了插件,閱讀插件提交和宣傳來學習如何讓它更廣泛的散布開來。
創(chuàng)建插件
本部分進入你需要遵守的幾個步驟,和一些考慮什么時候建立一個結構良好的 WordPress插件。
名字,文件和位置
插件名字
創(chuàng)建插件的第一個任務是想出插件要做什么,然后起一個(希望是唯一的)名字。 查看插件 和其他的相關庫,驗證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開發(fā)者選擇使用能描述插件功能的那個名字; 如,一個聯(lián)系到天氣的插件可能含有單詞"weather"在名字中。名字可以是好幾個詞。
插件文件
下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來。如,如果你的插件叫做"Fabulous Functionality",你可以把你的PHP文件起名為 fabfunc.php。試著取一個唯一的名字。人們安裝你的插件的時候會把這個PHP文件放到WordPress插件目錄中,wp-content/plugins/,所以沒有兩個不同的在使用的插件擁有相同的PHP文件名。
另外一個操作是把你的插件分成幾個文件,你的插件至少要有一個PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如fabfunc 和 fabfunc.php,就把所有的插件文件放到這個目錄下,然后告訴你的插件用戶安裝整個目錄到wp-content/plugins/下。
在這個文章的其余部分,"插件PHP文件"引用了主插件PHP文件,可以在 wp-content/plugins/ 或者一個子目錄下
自述文件
如果你想在http://wordpress.org/extend/plugins/上建立一個插件主機,你還需要用標準格式創(chuàng)建readme.txt文件,把它放到你的插件中去。參見 http://wordpress.org/extend/plugins/about/readme.txt 獲得這格式的描述.
主頁
創(chuàng)建一個網(wǎng)頁來作為你的插件的主頁也是非常有用的。這個頁面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個版本,版本更替的時候你的插件做出了哪些改動,如何使用插件等等。
文件標題
現(xiàn)在開始在你的插件的主PHP文件中添加信息。
標準插件信息
插件主PHP文件的頂部必須包含一個標準插件信息標題。這個標題讓WordPress 識別你的插件存在,添加到插件管理界面以便激活,載入,運行; 沒有標題,你的插件將不會被激活也不會運行,如下是標題格式:
<?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需要用來識別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來創(chuàng)建插件管理界面上的插件表格。這些行的順序并不重要。
許可
根據(jù)帶有許可信息這個標準標題是個慣例。多數(shù)插件使用WordPress 使用的GPL 許可,或者是一個 compatible with the GPL的許可。簡要說明下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)在是該讓你的插件做些什么的時候了。這個部分包含了一些一般的關于插件開發(fā)的思想,描述了如何完成你的插件需要完成的幾個任務。
WordPress 插件 Hooks
很多插件通過連接一個或者多個的WordPress 插件 "hooks"來完成它們的目標。 插件hooks的工作方式是在WordPress運行的時候,在各個不同的時候,WordPress檢查是否有插件在那時有注冊過的函數(shù)在此刻運行,如果有的話,函數(shù)運行。三個函數(shù)更改WordPress的默認行為。
例如,在WordPress添加文章標題到瀏覽器輸出的時候,首先檢查是否有任何插件為"filter"hook注冊了一個函數(shù)叫做"the_title"。如果有,標題文字會被每個注冊函數(shù)按順序傳遞,最終結果就是打印出來的結果。這樣,如果你的插件需要添加一些信息到打印標題,它可以注冊一個"the_title" filter函數(shù).
另一個例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁面的底部,它檢查看是否某個插件已經(jīng)為 "wp_footer" action hook注冊函數(shù),然后依次運行。
你可以了解更多關于如何為filter 和 action hooks注冊函數(shù),什么插件hooks在WordPress和在插件API中可用。如果你在WordPress代碼中發(fā)現(xiàn)一個你想使用action或者filter的地方,但是WordPress沒有,你可以建議使用新的hooks (建議通常都會被采納); 如何建議參見提交錯誤。
模板標簽
另外一種用插件添加WordPress功能的方法是通過建立自定義 模板標簽。有人想使用你的插件添加這些 "標簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標簽到文章中的插件,可以為邊欄定義一個叫做geotag_list_states()的模板標簽函數(shù),列出所有的有關國家,并帶有國家歸檔頁面的鏈接。
想要定義一個模板標簽,需要簡單的寫一個PHP 函數(shù)并為插件用戶在插件主頁或者是插件PHP主文件中寫下資料說明。在給函數(shù)做資料說明時,給出一個具體需要加入什么到主題文件中以使用函數(shù)的例子是個不錯的想法包括 <?php 和 ?>.
保持插件數(shù)據(jù)到數(shù)據(jù)庫
大多數(shù)插件需要從站長或者blog用戶得到一些信息并保存在sessions中,為了在它的filter 函數(shù), action 函數(shù)和模板函數(shù)中使用。這個信息必須保存在WordPress 數(shù)據(jù)庫中,為了在sessions中可以持久穩(wěn)定。有兩種基本方法可以保存插件數(shù)據(jù)到數(shù)據(jù)庫中:
- 使用WordPress "操作"機制(下有描述)。這個方法適合儲存相關的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設置插件時站長輸入的,今后幾乎不會更改的數(shù)據(jù)。
- 創(chuàng)建一個新的自定義的數(shù)據(jù)庫表格。這種方法適合聯(lián)合單獨的文章,頁面,附件或者評論的數(shù)據(jù)—一種隨著時間而增加的,沒有單獨的名字的數(shù)據(jù)。參見 創(chuàng)建插件表格.
WordPress 操作機制
參見創(chuàng)建操作頁面 得到關于如何創(chuàng)建自動為你保存操作的頁面的信息。
WordPress有一個保存,更新,重新得到個體, 制定的數(shù)據(jù)("操作")機制,在 WordPress 數(shù)據(jù)庫中。操作值可以是字符串,數(shù)組或者是PHP對象(它們將被 "連載",或者在存儲前轉變?yōu)樽址谥匦碌玫絺€體時反連載)。操作名字是字符串,它們必須是唯一的,這樣它們不會與其他 WordPress或者是插件起沖突。
這里是你的插件可以用來進入WordPress操作的主要函數(shù)。
add_option($name, $value, $description, $autoload);
- 創(chuàng)建一個新選項;如果選項存在就什么都不做
- $name
- Required (string). 將被添加的選項名字
- $value
- Optional (string), 默認為空的字符串。 將被存儲的選項值
- $description
- Optional (string), 默認為空的字符串。選項的描述,被放在wordpress數(shù)據(jù)庫中以防某些人瀏覽數(shù)據(jù)庫去看選項在哪。
- $autoload
- 選項,默認為'yes' (enum: 'yes' or 'no').如果設置為'yes',那么設置被get_alloptions函數(shù)自動檢索。
get_option($option);
- 從數(shù)據(jù)庫中檢索一個選項值
- $option
- Required (string). 你想要返回的選項值的選項的名稱。你能夠找到被安裝在相關選項中的一列默認選項
update_option($option_name, $newvalue);
- 在數(shù)據(jù)庫里更新或者創(chuàng)建一個選項值(注:如果你不想使用$description 或者 $autoload參數(shù),add_option就沒必要被訪問。)
- $option_name
- Required (string). 選項名稱更新。
- $newvalue
- Required. 為選項賦予新的值。
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.
管理面板
假設你的插件有一些操作存儲在WordPress數(shù)據(jù)庫中 (參見上面的部分),你也許希望有一個管理面板,允許你的插件用戶來查看和編輯操作。方法可以在 添加管理目錄中找到。
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.
讓你的插件國際化
一旦你插件的程序完成了,另一個需要考慮的是(假設你打算發(fā)布你的插件) 國際化. 國際化是建立軟件的一個過程,這樣它才可以本地化 ;本地化是翻譯軟件的顯示文本為其他語言。WordPress是全世界使用的,所以它有國際化和本地化版本,包括本地化的插件。想要知道關于WordPress的 GNU gettext的本地化使用,參見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.
強力推薦你國際化你的插件,這樣來自不同國家的用戶,可以本地化插件,過程如下:
- 給插件選擇一個翻譯"文本區(qū)域"名字。這通常和你的插件文件名字一樣(除了 .php), 而且必須是用戶安裝過的插件中唯一的。
- 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個或者兩個 WordPress gettext 函數(shù)把它們包圍起來。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 $domain 這部分空白).
- __($message, $domain)
- 使用現(xiàn)有本地$domain翻譯 $message。換行你將要用來用這個函數(shù)計算的文字字符串。
- _e($message, $domain)
- 使用現(xiàn)有本地$domain翻譯 $message, 然后在屏幕上顯示出來,換行你使用這個函數(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)建一個POT文件(翻譯目錄列出了所有的可以翻譯的信息),隨著你的插件分發(fā)出去。用戶將需要把他們的翻譯過的MO文件放在你的插件的PHP文件的同一目錄下,命名為domain-ll_CC.mo,那里ll_CC是本地文件的名字。參見翻譯WordPress以獲得關于POT文件,MO文件和本地文件的信息。
- 通過在調用gettext函數(shù)之前調用load_plugin_textdomain為現(xiàn)有本地文件和你的文本區(qū)域文件載入翻譯,在session中越晚越好,(因為一些多語言插件載入時改變了本地文件)。一個可能的辦法是定義一個初始化函數(shù),在所有插件函數(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');
如果你閱讀了這個部分因為你想要國際化一個主題,你大體上可以按照上面的步驟來,除非:
- MO文件在主題目錄中(與style.css同樣的位置).
- MO 文件命名為ll_CC.mo, ll_CC是本地文件名(這個域不是文件名字的一部分).
- 想要載入文本域,加入如下代碼(在某個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.
插件開發(fā)建議
最后一部分包括一些關于插件開發(fā)的建議。
- 插件代碼應該遵循 WordPress編碼標準。請參照內嵌文檔標準。
- 所有的插件中的函數(shù)需要有唯一的名字,因為這個原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個很好的主意。另一個可能性是在類中定義你的插件函數(shù)(也需要有唯一名字).
- 不要吧WordPress數(shù)據(jù)庫表格前綴(通常是"wp_")放入你的插件中。確認使用不同的$wpdb->前綴來代替。
- 數(shù)據(jù)庫閱讀很簡單但是寫確很難,數(shù)據(jù)庫是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫中做改動,卻是一個復雜的多的過程,很難。所以試著最小化寫入的數(shù)量。首先在你的代碼中準備好一切,這樣你只需要選擇那些你需要的寫操作。
- 只選擇你需要的。盡管數(shù)據(jù)庫取數(shù)據(jù)非??欤阈枰囍鴾p少數(shù)據(jù)庫的負載,通過只選擇你需要的數(shù)據(jù)。如果你需要知道一個表格有多少行,不要SELECT * FROM,因為所有行中的所有數(shù)據(jù)都會被牽扯到,浪費內存。這樣,如果你只需要插件中的post_id 和post_author,只要SELECT那些特定的區(qū)域,來減小數(shù)據(jù)庫負載。記住: 大量的其他進程可能同時需要使用數(shù)據(jù)庫。數(shù)據(jù)庫和服務器每個都有大量的資源供這些進程使用。了解如何減小你的插件需要的資源會保證你的插件不會是被人說成濫用資源的哪種。
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插件開發(fā)人員使用的Jquery的簡單化AJAX(10APR08)
- (10MAR08)
- 12 part "如何寫Wordpress 插件" at DevLounge.net ,來自 Ronald Huereca (PDF)
- 如何創(chuàng)建WordPress插件 (9AUG07)
- your WordPress 插件中使用AJAX, DevLounce.net上也有 (25MAY07)
- (22FEB05)