WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國十大主機(jī)
編輯“WordPress:Writing a Plugin”
該編輯可以被撤銷。 請(qǐng)檢查下面的對(duì)比以核實(shí)您想要撤銷的內(nèi)容,然后發(fā)布下面的更改以完成撤銷。
最后版本 | 您的文本 | ||
第1行: | 第1行: | ||
= Introduction = | |||
? | |||
Prior to WordPress Version 1.2, if you wanted to modify the behavior of WordPress, you had to edit (or "hack") the WordPress source code. However, in more current versions of WordPress, you can easily modify and add to the functionality of the core version of WordPress by using [[WordPress:Plugins]]. The basic idea of the plugin architecture is to keep the core of WordPress relatively simple, but flexible enough that nearly every aspect of its input and output can be modified by plugins. Here is a definition: | |||
? | |||
;WordPress Plugin : A WordPress plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress [[WordPress:Plugin API|Plugin Application Program Interface (API)]]. | |||
? | |||
If you find yourself wishing that WordPress had some new or modified functionality, the first thing to do is to search various plugin repositories (which you can learn about in the [[WordPress:Plugins]] article), and see if someone has already created a plugin that suits your needs. If not, this article will guide you through the process of creating your own plugins. | |||
? | |||
This article assumes you are already familiar with the basic functionality of WordPress, and PHP programming. | |||
? | |||
= 介紹 = | = 介紹 = | ||
第10行: | 第18行: | ||
本文假設(shè)你已經(jīng)熟悉基本的WordPress功能以及 PHP編程。 | 本文假設(shè)你已經(jīng)熟悉基本的WordPress功能以及 PHP編程。 | ||
== Resources == | |||
* There is a comprehensive list of articles and resources for plugin developers, including external articles on writing plugins, and articles on special topics, in [[WordPress:Plugin Resources]]. | |||
* Another good way to learn about plugins is to look at the PHP source code for well-written plugins, such as [[WordPress:Plugins#Default Plugins|Hello Dolly]], a plugin that is distributed with WordPress. | |||
* Once you have written your plugin, read [[WordPress:Plugin Submission and Promotion]] to learn how to distribute it widely. | |||
== 資源 == | == 資源 == | ||
* 有很多插件開發(fā)者的的全面的文章列表和資源,[[WordPress:Plugin Resources|插件資源]] | * 有很多插件開發(fā)者的的全面的文章列表和資源,[[WordPress:Plugin Resources|插件資源]]中包括有關(guān)編寫插件的外部文章以及關(guān)于特比話題的文章。 | ||
* 另外一個(gè)了解有關(guān)插件的好方法是看寫的好的插件的PHP 源代碼,如 [[WordPress:Plugins#Default Plugins|Hello Dolly]], 一個(gè)分布式插件。 ? | * 另外一個(gè)了解有關(guān)插件的好方法是看寫的好的插件的PHP 源代碼,如 [[WordPress:Plugins#Default Plugins|Hello Dolly]], 一個(gè)分布式插件。 ? | ||
* 一旦你寫了插件,閱讀[[WordPress:Plugin Submission and Promotion|插件提交和宣傳]]來學(xué)習(xí)如何讓它更廣泛的散布開來。 | * 一旦你寫了插件,閱讀[[WordPress:Plugin Submission and Promotion|插件提交和宣傳]]來學(xué)習(xí)如何讓它更廣泛的散布開來。 | ||
= Creating a Plugin = | |||
This section of the article goes through the steps you need to follow, and things to consider when creating a well-structured WordPress plugin. | |||
= 創(chuàng)建插件 = | = 創(chuàng)建插件 = | ||
本部分進(jìn)入你需要遵守的幾個(gè)步驟,和一些考慮什么時(shí)候建立一個(gè)結(jié)構(gòu)良好的 WordPress插件。 | 本部分進(jìn)入你需要遵守的幾個(gè)步驟,和一些考慮什么時(shí)候建立一個(gè)結(jié)構(gòu)良好的 WordPress插件。 | ||
== Names, Files, and Locations == | |||
=== Plugin Name === | |||
The first task in creating a plugin is to think about what the plugin will do, and make a (hopefully unique) name for your plugin. Check out [[WordPress:Plugins]] and the other repositories it refers to, to verify that your name is unique; you might also do a Google search on your proposed name. Most plugin developers choose to use names that somewhat describe what the plugin does; for instance, a weather-related plugin would probably have the word "weather" in the name. The name can be multiple words. | |||
== 名字,文件和位置 == | == 名字,文件和位置 == | ||
第24行: | 第49行: | ||
=== 插件名字 === | === 插件名字 === | ||
創(chuàng)建插件的第一個(gè)任務(wù)是想出插件要做什么,然后起一個(gè)(希望是唯一的)名字。 查看[[WordPress:Plugins|插件]] 和其他的相關(guān)庫,驗(yàn)證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開發(fā)者選擇使用能描述插件功能的那個(gè)名字; | 創(chuàng)建插件的第一個(gè)任務(wù)是想出插件要做什么,然后起一個(gè)(希望是唯一的)名字。 查看[[WordPress:Plugins|插件]] 和其他的相關(guān)庫,驗(yàn)證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開發(fā)者選擇使用能描述插件功能的那個(gè)名字; 如,一個(gè)聯(lián)系到天氣的插件啃含有單詞"weather"在名字中。名字可以是好幾個(gè)詞。 | ||
? | |||
=== Plugin Files === | |||
? | |||
The next step is to create a PHP file with a name derived from your chosen plugin name. For instance, if your plugin will be called "Fabulous Functionality", you might call your PHP file <tt>fabfunc.php</tt>. Again, try to choose a unique name. People who install your plugin will be putting this PHP file into the WordPress plugin directory in their installation, <tt>wp-content/plugins/</tt>, so no two plugins they are using can have the same PHP file name. | |||
=== 插件文件 === | === 插件文件 === | ||
下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來。如,如果你的插件叫做"Fabulous Functionality" | 下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來。如,如果你的插件叫做"Fabulous Functionality",你可以吧你的PHP文件起名為 <tt>fabfunc.php</tt>。試著取一個(gè)唯一的名字。人們安裝你的插件的時(shí)候會(huì)把這個(gè)PHP文件放到WordPress插件目錄中,<tt>wp-content/plugins/</tt>,所以沒有兩個(gè)不同的在使用的插件擁有相同的PHP文件名。 ? | ||
? | |||
Another option is to split your plugin into multiple files. Your plugin must have at least one PHP file; it could also contain JavaScript files, CSS files, image files, language files, etc. If there are multiple files, pick a unique name for a file directory and for the main PHP file, such as <tt>fabfunc</tt> and <tt>fabfunc.php</tt> in this example, put all your plugin's files into that directory, and tell your plugin users to install the whole directory under <tt>wp-content/plugins/</tt>. | |||
? | |||
In the rest of this article, "the plugin PHP file" refers to the main plugin PHP file, whether in <tt>wp-content/plugins/</tt> or a sub-directory. | |||
另外一個(gè)操作是把你的插件分成幾個(gè)文件,你的插件至少要有一個(gè)PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如<tt>fabfunc</tt> 和 <tt>fabfunc.php</tt>,就把所有的插件文件放到這個(gè)目錄下,然后告訴你的插件用戶安裝整個(gè)目錄到<tt>wp-content/plugins/</tt>下。 | 另外一個(gè)操作是把你的插件分成幾個(gè)文件,你的插件至少要有一個(gè)PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如<tt>fabfunc</tt> 和 <tt>fabfunc.php</tt>,就把所有的插件文件放到這個(gè)目錄下,然后告訴你的插件用戶安裝整個(gè)目錄到<tt>wp-content/plugins/</tt>下。 | ||
在這個(gè)文章的其余部分,"插件PHP文件"引用了主插件PHP文件,可以在 <tt>wp-content/plugins/</tt> 或者一個(gè)子目錄下 | 在這個(gè)文章的其余部分,"插件PHP文件"引用了主插件PHP文件,可以在 <tt>wp-content/plugins/</tt> 或者一個(gè)子目錄下 | ||
=== Readme File === | |||
If you want to host your plugin on http://wordpress.org/extend/plugins/, you also need to create a <tt>readme.txt</tt> file in a standard format, and include it with your plugin. See http://wordpress.org/extend/plugins/about/readme.txt for a description of the format. | |||
=== 自述文件 === | === 自述文件 === | ||
如果你想在http://wordpress.org/extend/plugins/上建立一個(gè)插件主機(jī),你還需要用標(biāo)準(zhǔn)格式創(chuàng)建<tt>readme.txt</tt>文件,把它放到你的插件中去。參見 http://wordpress.org/extend/plugins/about/readme.txt 獲得這格式的描述. | 如果你想在http://wordpress.org/extend/plugins/上建立一個(gè)插件主機(jī),你還需要用標(biāo)準(zhǔn)格式創(chuàng)建<tt>readme.txt</tt>文件,把它放到你的插件中去。參見 http://wordpress.org/extend/plugins/about/readme.txt 獲得這格式的描述. | ||
=== Home Page === | |||
It is also very useful to create a web page to act as the home page for your plugin. This page should describe how to install the plugin, what it does, what versions of WordPress it is compatible with, what has changed from version to version of your plugin, and how to use the plugin. | |||
=== 主頁 === | === 主頁 === | ||
創(chuàng)建一個(gè)網(wǎng)頁來作為你的插件的主頁也是非常有用的。這個(gè)頁面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個(gè)版本,版本更替的時(shí)候你的插件做出了哪些改動(dòng),如何使用插件等等。 | 創(chuàng)建一個(gè)網(wǎng)頁來作為你的插件的主頁也是非常有用的。這個(gè)頁面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個(gè)版本,版本更替的時(shí)候你的插件做出了哪些改動(dòng),如何使用插件等等。 | ||
== File Headers == | |||
Now it's time to put some information into your main plugin PHP file. | |||
== 文件標(biāo)題 == | == 文件標(biāo)題 == | ||
現(xiàn)在開始在你的插件的主PHP文件中添加信息。 | 現(xiàn)在開始在你的插件的主PHP文件中添加信息。 | ||
=== Standard Plugin Information === | |||
The top of your plugin's main PHP file must contain a standard plugin information header. This header lets WordPress recognize that your plugin exists, add it to the plugin management screen so it can be activated, load it, and run its functions; without the header, your plugin will never be activated and will never run. Here is the header format: | |||
<pre> | |||
<?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 | |||
*/ | |||
?> | |||
</pre> | |||
The minimum information WordPress needs to recognize your plugin is the Plugin Name line. The rest of the information (if present) will be used to create the table of plugins on the plugin management screen. The order of the lines is not important. | |||
=== 標(biāo)準(zhǔn)插件信息 === | === 標(biāo)準(zhǔn)插件信息 === | ||
第64行: | 第128行: | ||
WordPress需要用來識(shí)別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來創(chuàng)建插件管理界面上的插件表格。這些行的順序并不重要。 | WordPress需要用來識(shí)別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來創(chuàng)建插件管理界面上的插件表格。這些行的順序并不重要。 | ||
=== License === | |||
It is customary to follow the standard header with information about licensing for the plugin. Most plugins use the [http://www.gnu.org/copyleft/gpl.html GPL] license used by WordPress or a license [http://www.fsf.org/licensing/licenses/index_html#GPLCompatibleLicenses compatible with the GPL]. To indicate a GPL license, include the following lines in your plugin: | |||
<pre> | |||
<?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 | |||
*/ | |||
?> | |||
</pre> | |||
=== 許可 === | === 許可 === | ||
第89行: | 第178行: | ||
?> | ?> | ||
</pre> | </pre> | ||
== Programming Your Plugin == | |||
Now, it's time to make your plugin actually do something. This section contains some general ideas about plugin development, and describes how to accomplish several tasks your plugin will need to do. | |||
== 規(guī)劃你的插件 == | == 規(guī)劃你的插件 == | ||
現(xiàn)在是該讓你的插件做些什么的時(shí)候了。這個(gè)部分包含了一些一般的關(guān)于插件開發(fā)的思想,描述了如何完成你的插件需要完成的幾個(gè)任務(wù)。 | 現(xiàn)在是該讓你的插件做些什么的時(shí)候了。這個(gè)部分包含了一些一般的關(guān)于插件開發(fā)的思想,描述了如何完成你的插件需要完成的幾個(gè)任務(wù)。 | ||
=== WordPress Plugin Hooks === | |||
Many plugins accomplish their goals by connecting to one or more WordPress plugin "hooks". The way plugin hooks work is that at various times while WordPress is running, WordPress checks to see if any plugins have registered functions to run at that time, and if so, the functions are run. These functions modify the default behavior of WordPress. | |||
=== WordPress 插件 Hooks === | === WordPress 插件 Hooks === | ||
很多插件通過連接一個(gè)或者多個(gè)的WordPress 插件 "hooks"來完成它們的目標(biāo)。 插件hooks的工作方式是在WordPress運(yùn)行的時(shí)候,在各個(gè)不同的時(shí)候,WordPress檢查是否有插件在那時(shí)有注冊(cè)過的函數(shù)在此刻運(yùn)行,如果有的話,函數(shù)運(yùn)行。三個(gè)函數(shù)更改WordPress的默認(rèn)行為。 | 很多插件通過連接一個(gè)或者多個(gè)的WordPress 插件 "hooks"來完成它們的目標(biāo)。 插件hooks的工作方式是在WordPress運(yùn)行的時(shí)候,在各個(gè)不同的時(shí)候,WordPress檢查是否有插件在那時(shí)有注冊(cè)過的函數(shù)在此刻運(yùn)行,如果有的話,函數(shù)運(yùn)行。三個(gè)函數(shù)更改WordPress的默認(rèn)行為。 | ||
For instance, before WordPress adds the title of a post to browser output, it first checks to see if any plugin has registered a function for the "filter" hook called "the_title". If so, the title text is passed in turn through each registered function, and the final result is what is printed. So, if your plugin needs to add some information to the printed title, it can register a "the_title" filter function. | |||
例如,在WordPress添加文章標(biāo)題到瀏覽器輸出的時(shí)候,首先檢查是否有任何插件為"filter"hook注冊(cè)了一個(gè)函數(shù)叫做"the_title"。如果有,標(biāo)題文字會(huì)被每個(gè)注冊(cè)函數(shù)按順序傳遞,最終結(jié)果就是打印出來的結(jié)果。這樣,如果你的插件需要添加一些信息到打印標(biāo)題,它可以注冊(cè)一個(gè)"the_title" filter函數(shù). | 例如,在WordPress添加文章標(biāo)題到瀏覽器輸出的時(shí)候,首先檢查是否有任何插件為"filter"hook注冊(cè)了一個(gè)函數(shù)叫做"the_title"。如果有,標(biāo)題文字會(huì)被每個(gè)注冊(cè)函數(shù)按順序傳遞,最終結(jié)果就是打印出來的結(jié)果。這樣,如果你的插件需要添加一些信息到打印標(biāo)題,它可以注冊(cè)一個(gè)"the_title" filter函數(shù). | ||
Another example is the "action" hook called "wp_footer". Just before the end of the HTML page WordPress is generating, it checks to see whether any plugins have registered functions for the "wp_footer" action hook, and runs them in turn. | |||
You can learn more about how to register functions for both filter and action hooks, and what plugin hooks are available in WordPress, in [[WordPress:Plugin API]]. If you find a spot in the WordPress code where you'd like to have an action or filter, but WordPress doesn't have one, you can also suggest new hooks (suggestions will generally be taken); see [[WordPress:Reporting Bugs]] to find out how. | |||
另一個(gè)例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁面的底部,它檢查看是否某個(gè)插件已經(jīng)為 "wp_footer" action hook注冊(cè)函數(shù),然后依次運(yùn)行。 | 另一個(gè)例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁面的底部,它檢查看是否某個(gè)插件已經(jīng)為 "wp_footer" action hook注冊(cè)函數(shù),然后依次運(yùn)行。 | ||
You can learn more about how to register functions for both filter and action hooks, and what plugin hooks are available in WordPress, in [[WordPress:Plugin API]]. If you find a spot in the WordPress code where you'd like to have an action or filter, but WordPress doesn't have one, you can also suggest new hooks (suggestions will generally be taken); see [[WordPress:Reporting Bugs]] to find out how. | |||
你可以了解更多關(guān)于如何為filter 和 action hooks注冊(cè)函數(shù),什么插件hooks在WordPress和在[[WordPress:Plugin API|插件API]]中可用。如果你在WordPress代碼中發(fā)現(xiàn)一個(gè)你想使用action或者filter的地方,但是WordPress沒有,你可以建議使用新的hooks (建議通常都會(huì)被采納); 如何建議參見[[WordPress:Reporting Bugs|提交錯(cuò)誤]]。 | 你可以了解更多關(guān)于如何為filter 和 action hooks注冊(cè)函數(shù),什么插件hooks在WordPress和在[[WordPress:Plugin API|插件API]]中可用。如果你在WordPress代碼中發(fā)現(xiàn)一個(gè)你想使用action或者filter的地方,但是WordPress沒有,你可以建議使用新的hooks (建議通常都會(huì)被采納); 如何建議參見[[WordPress:Reporting Bugs|提交錯(cuò)誤]]。 | ||
=== Template Tags === | |||
Another way for a plugin to add functionality to WordPress is by creating custom [[WordPress:Template Tags]]. Someone who wants to use your plugin can add these "tags" to their theme, in the sidebar, post content section, or wherever it is appropriate. For instance, a plugin that adds geographical tags to posts might define a template tag function called <tt>geotag_list_states()</tt> for the sidebar, which lists all the states posts are tagged with, with links to the state-based archive pages the plugin enables. | |||
To define a custom template tag, simply write a PHP function and document it for plugin users on your plugin's home page and/or in the plugin's main PHP file. It's a good idea when documenting the function to give an example of exactly what needs to be added to the theme file to use the function, including the <tt><?php</tt> and <tt>?></tt>. | |||
=== 模板標(biāo)簽 === | === 模板標(biāo)簽 === | ||
另外一種用插件添加WordPress功能的方法是通過建立自定義 [[WordPress:Template Tags|模板標(biāo)簽]]。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個(gè)叫做<tt>geotag_list_states()</tt> | 另外一種用插件添加WordPress功能的方法是通過建立自定義 [[WordPress:Template Tags|模板標(biāo)簽]]。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個(gè)叫做<tt>geotag_list_states()</tt>的模板標(biāo)簽函數(shù),列出所有的有關(guān)國家,并帶有國家文章頁面的鏈接。 | ||
想要定義一個(gè)模板標(biāo)簽,需要簡單的寫一個(gè)PHP 函數(shù)并為插件用戶在插件主頁或者是插件PHP主文件中寫下資料說明。在給函數(shù)做資料說明時(shí),給出一個(gè)具體需要加入什么到主題文件中以使用函數(shù)的例子是個(gè)不錯(cuò)的想法包括 <tt><?php</tt> 和 <tt>?></tt>. | 想要定義一個(gè)模板標(biāo)簽,需要簡單的寫一個(gè)PHP 函數(shù)并為插件用戶在插件主頁或者是插件PHP主文件中寫下資料說明。在給函數(shù)做資料說明時(shí),給出一個(gè)具體需要加入什么到主題文件中以使用函數(shù)的例子是個(gè)不錯(cuò)的想法包括 <tt><?php</tt> 和 <tt>?></tt>. | ||
===Saving Plugin Data to the Database=== | |||
Most plugins will need to get some input from the site owner or blog users and save it between sessions, for use in its filter functions, action functions, and template functions. This information has to be saved in the WordPress database, in order to be persistent between sessions. There are two basic methods for saving plugin data in the database: | |||
# Use the WordPress "option" mechanism (described below). This method is appropriate for storing relatively small amounts of relatively static, named pieces of data -- the type of data you'd expect the site owner to enter when first setting up the plugin, and rarely change thereafter. | |||
# Create a new, custom database table. This method is appropriate for data associated with individual posts, pages, attachments, or comments -- the type of data that will grow as time goes on, and that doesn't have individual names. See [[WordPress:Creating Tables with Plugins]] for information on how to do this. | |||
===保持插件數(shù)據(jù)到數(shù)據(jù)庫=== | ===保持插件數(shù)據(jù)到數(shù)據(jù)庫=== | ||
第115行: | 第232行: | ||
# 使用WordPress "操作"機(jī)制(下有描述)。這個(gè)方法適合儲(chǔ)存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時(shí)站長輸入的,今后幾乎不會(huì)更改的數(shù)據(jù)。 | # 使用WordPress "操作"機(jī)制(下有描述)。這個(gè)方法適合儲(chǔ)存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時(shí)站長輸入的,今后幾乎不會(huì)更改的數(shù)據(jù)。 | ||
# 創(chuàng)建一個(gè)新的自定義的數(shù)據(jù)庫表格。這種方法適合聯(lián)合單獨(dú)的文章,頁面,附件或者評(píng)論的數(shù)據(jù)—一種隨著時(shí)間而增加的,沒有單獨(dú)的名字的數(shù)據(jù)。參見 [[WordPress:Creating Tables with Plugins|創(chuàng)建插件表格]]. | # 創(chuàng)建一個(gè)新的自定義的數(shù)據(jù)庫表格。這種方法適合聯(lián)合單獨(dú)的文章,頁面,附件或者評(píng)論的數(shù)據(jù)—一種隨著時(shí)間而增加的,沒有單獨(dú)的名字的數(shù)據(jù)。參見 [[WordPress:Creating Tables with Plugins|創(chuàng)建插件表格]]. | ||
=== WordPress Options Mechanism === | |||
See [[WordPress:Creating Options Pages]] for info on how to create a page that will automatically save your options for you. | |||
WordPress has a mechanism for saving, updating, and retrieving individual, named pieces of data ("options") in the WordPress database. Option values can be strings, arrays, or PHP objects (they will be "serialized", or converted to a string, before storage, and unserialized when retrieved). Option names are strings, and they must be unique, so that they do not conflict with either WordPress or other plugins. | |||
Here are the main functions your plugin can use to access WordPress options. | |||
<pre> | |||
add_option($name, $value, $description, $autoload); | |||
</pre> | |||
: 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 <tt>get_alloptions</tt> function. | |||
<pre> | |||
get_option($option); | |||
</pre> | |||
: 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]]. | |||
<pre> | |||
update_option($option_name, $newvalue); | |||
</pre> | |||
: Updates or creates an option value in the database (note that <tt>add_option</tt> does not have to be called if you do not want to use the <tt>$description</tt> or <tt>$autoload</tt> parameters). | |||
;$option_name: Required (string). Name of the option to update. | |||
;$newvalue: Required.? The new value for the option. | |||
=== WordPress 操作機(jī)制=== | === WordPress 操作機(jī)制=== | ||
第127行: | 第274行: | ||
add_option($name, $value, $description, $autoload); | add_option($name, $value, $description, $autoload); | ||
</pre> | </pre> | ||
: | : Creates a new option; does nothing if option already exists. | ||
;$name: Required (string). | ;$name: Required (string). Name of the option to be added. | ||
;$value: Optional (string), | ;$value: Optional (string), defaults to empty string. The option value to be stored. | ||
;$description: Optional (string), | ;$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: | ;$autoload: Optional, defaults to 'yes' (enum: 'yes' or 'no'). If set to 'yes' the setting is automatically retrieved by the <tt>get_alloptions</tt> function. | ||
? | |||
<pre> | <pre> | ||
get_option($option); | get_option($option); | ||
</pre> | </pre> | ||
: | : Retrieves an option value from the database. | ||
;$option: Required (string).? | ;$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]]. | ||
? | |||
<pre> | <pre> | ||
update_option($option_name, $newvalue); | update_option($option_name, $newvalue); | ||
</pre> | </pre> | ||
: | : Updates or creates an option value in the database (note that <tt>add_option</tt> does not have to be called if you do not want to use the <tt>$description</tt> or <tt>$autoload</tt> parameters). | ||
;$option_name: Required (string). | ;$option_name: Required (string). Name of the option to update. | ||
;$newvalue: Required.? | ;$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ù)庫中 (參見上面的部分),你也許希望有一個(gè)管理面板,允許你的插件用戶來查看和編輯操作。方法可以在 [[WordPress:Adding Administration Menus|添加管理目錄]]中找到。 | 假設(shè)你的插件有一些操作存儲(chǔ)在WordPress數(shù)據(jù)庫中 (參見上面的部分),你也許希望有一個(gè)管理面板,允許你的插件用戶來查看和編輯操作。方法可以在 [[WordPress:Adding Administration Menus|添加管理目錄]]中找到。 | ||
== 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]]. | |||
== 讓你的插件國際化 == | == 讓你的插件國際化 == | ||
一旦你插件的程序完成了,另一個(gè)需要考慮的是(假設(shè)你打算發(fā)布你的插件) ''國際化''. 國際化是建立軟件的一個(gè)過程,這樣它才可以''本地化'' ;本地化是翻譯軟件的顯示文本為其他語言。WordPress是全世界使用的,所以它有國際化和本地化版本,包括本地化的插件。想要知道關(guān)于WordPress的 GNU gettext的本地化使用,參見[[WordPress:Translating WordPress]]. ? | 一旦你插件的程序完成了,另一個(gè)需要考慮的是(假設(shè)你打算發(fā)布你的插件) ''國際化''. 國際化是建立軟件的一個(gè)過程,這樣它才可以''本地化'' ;本地化是翻譯軟件的顯示文本為其他語言。WordPress是全世界使用的,所以它有國際化和本地化版本,包括本地化的插件。想要知道關(guān)于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 <tt>.php</tt>), 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 <tt>$domain</tt> argument blank). | |||
; <tt>__($message, $domain)</tt> : Translates <tt>$message</tt> using the current locale for <tt>$domain</tt>. Wrap text strings that you are going to use in calculations with this function. | |||
; <tt>_e($message, $domain)</tt> : Translates <tt>$message</tt> using the current locale for <tt>$domain</tt>, and then prints it on the screen. Wrap text strings that you are directly printed with this function. | |||
強(qiáng)力推薦你國際化你的插件,這樣來自不同國家的用戶,可以本地化插件,過程如下: | 強(qiáng)力推薦你國際化你的插件,這樣來自不同國家的用戶,可以本地化插件,過程如下: | ||
第155行: | 第319行: | ||
* 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個(gè)或者兩個(gè) WordPress gettext 函數(shù)把它們包圍起來。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 <tt>$domain</tt> 這部分空白). | * 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個(gè)或者兩個(gè) WordPress gettext 函數(shù)把它們包圍起來。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 <tt>$domain</tt> 這部分空白). | ||
; <tt>__($message, $domain)</tt> :使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>。換行你將要用來用這個(gè)函數(shù)計(jì)算的文字字符串。 | ; <tt>__($message, $domain)</tt> :使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>。換行你將要用來用這個(gè)函數(shù)計(jì)算的文字字符串。 | ||
; <tt>_e($message, $domain)</tt> : 使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>, | ; <tt>_e($message, $domain)</tt> : 使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>, 然后在屏幕上顯示出來,換行你使用這個(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 <tt>domain-ll_CC.mo</tt>, where <tt>ll_CC</tt> 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 <tt>load_plugin_textdomain</tt> 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": | |||
<pre> | |||
$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'); | |||
} | |||
</pre> | |||
If your plugin is in its own subdirectory, append that to the second argument of <tt>load_plugin_textdomain</tt>. | |||
* 創(chuàng)建一個(gè)POT文件(翻譯目錄列出了所有的可以翻譯的信息),隨著你的插件分發(fā)出去。用戶將需要把他們的翻譯過的MO文件放在你的插件的PHP文件的同一目錄下,命名為<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。參見[[WordPress:Translating WordPress|翻譯WordPress]]以獲得關(guān)于POT文件,MO文件和本地文件的信息。 | * 創(chuàng)建一個(gè)POT文件(翻譯目錄列出了所有的可以翻譯的信息),隨著你的插件分發(fā)出去。用戶將需要把他們的翻譯過的MO文件放在你的插件的PHP文件的同一目錄下,命名為<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。參見[[WordPress:Translating WordPress|翻譯WordPress]]以獲得關(guān)于POT文件,MO文件和本地文件的信息。 | ||
第176行: | 第358行: | ||
</pre> | </pre> | ||
If your plugin is in its own subdirectory, append that to the second argument of <tt>load_plugin_textdomain</tt> | If your plugin is in its own subdirectory, append that to the second argument of <tt>load_plugin_textdomain</tt> | ||
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 <tt>ll_CC.mo</tt>, where <tt>ll_CC</tt> 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: | |||
<pre> | |||
load_theme_textdomain('your_domain'); | |||
</pre> | |||
如果你閱讀了這個(gè)部分因?yàn)槟阆胍獓H化一個(gè)主題,你大體上可以按照上面的步驟來,除非: | 如果你閱讀了這個(gè)部分因?yàn)槟阆胍獓H化一個(gè)主題,你大體上可以按照上面的步驟來,除非: | ||
第184行: | 第374行: | ||
load_theme_textdomain('your_domain'); | load_theme_textdomain('your_domain'); | ||
</pre> | </pre> | ||
= 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 <tt>$wpdb->prefix</tt> 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 <em>writing</em> 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 <tt>SELECT * FROM</tt>, 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 <tt>SELECT</tt> 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ā)建議 = | ||
第191行: | 第391行: | ||
* 插件代碼應(yīng)該遵循[[WordPress:WordPress Coding Standards| WordPress編碼標(biāo)準(zhǔn)]]。請(qǐng)參照[[WordPress:Inline Documentation|內(nèi)嵌文檔]]標(biāo)準(zhǔn)。 | * 插件代碼應(yīng)該遵循[[WordPress:WordPress Coding Standards| WordPress編碼標(biāo)準(zhǔn)]]。請(qǐng)參照[[WordPress:Inline Documentation|內(nèi)嵌文檔]]標(biāo)準(zhǔn)。 | ||
* 所有的插件中的函數(shù)需要有唯一的名字,因?yàn)檫@個(gè)原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個(gè)很好的主意。另一個(gè)可能性是在類中定義你的插件函數(shù)(也需要有唯一名字). | * 所有的插件中的函數(shù)需要有唯一的名字,因?yàn)檫@個(gè)原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個(gè)很好的主意。另一個(gè)可能性是在類中定義你的插件函數(shù)(也需要有唯一名字). | ||
* | * 不要吧WordPress數(shù)據(jù)庫表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的<tt>$wpdb->前綴</tt>來代替。 ? | ||
* 數(shù)據(jù)庫閱讀很簡單但是寫確很難,數(shù)據(jù)庫是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫中做改動(dòng),卻是一個(gè)復(fù)雜的多的過程,很難。所以試著最小化<em>寫入</em>的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫操作。 | * 數(shù)據(jù)庫閱讀很簡單但是寫確很難,數(shù)據(jù)庫是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫中做改動(dòng),卻是一個(gè)復(fù)雜的多的過程,很難。所以試著最小化<em>寫入</em>的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫操作。 | ||
* 只選擇你需要的。盡管數(shù)據(jù)庫取數(shù)據(jù)非??欤阈枰囍鴾p少數(shù)據(jù)庫的負(fù)載,通過只選擇你需要的數(shù)據(jù)。如果你需要知道一個(gè)表格有多少行,不要<tt>SELECT * FROM</tt>,因?yàn)樗行兄械乃袛?shù)據(jù)都會(huì)被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的區(qū)域,來減小數(shù)據(jù)庫負(fù)載。記住: 大量的其他進(jìn)程可能同時(shí)需要使用數(shù)據(jù)庫。數(shù)據(jù)庫和服務(wù)器每個(gè)都有大量的資源供這些進(jìn)程使用。了解如何減小你的插件需要的資源會(huì)保證你的插件不會(huì)是被人說成濫用資源的哪種。 | * 只選擇你需要的。盡管數(shù)據(jù)庫取數(shù)據(jù)非???,你需要試著減少數(shù)據(jù)庫的負(fù)載,通過只選擇你需要的數(shù)據(jù)。如果你需要知道一個(gè)表格有多少行,不要<tt>SELECT * FROM</tt>,因?yàn)樗行兄械乃袛?shù)據(jù)都會(huì)被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的區(qū)域,來減小數(shù)據(jù)庫負(fù)載。記住: 大量的其他進(jìn)程可能同時(shí)需要使用數(shù)據(jù)庫。數(shù)據(jù)庫和服務(wù)器每個(gè)都有大量的資源供這些進(jìn)程使用。了解如何減小你的插件需要的資源會(huì)保證你的插件不會(huì)是被人說成濫用資源的哪種。 | ||
= External Resources = | |||
* [http://amiworks.co.in/talk/simplified-ajax-for-wordpress-plugin-developers-using-jquery/ Simplified AJAX For WordPress Plugin Developers using Jquery](10APR08) | |||
* [http://www.rafaeldohms.com.br/2008/03/10/desenvolvendo-plugins-para-wordpress/pt/ "Desenvolvendo Plugins para WordPress" by Rafael Dohms (in Brazilian Portuguese)] (10MAR08) | |||
* [http://www.devlounge.net/extras/how-to-write-a-wordpress-plugin 12 part "How to Write a Wordpress Plugin" at DevLounge.net] by [http://ronalfy.com Ronald Huereca] ([http://www.devlounge.net/publik/Devlounge%20-%20How%20to%20Write%20a%20Wordpress%20Plugin.pdf PDF]) | |||
* [http://ditio.net/2007/08/09/how-to-create-wordpress-plugin-from-a-scratch/ How to create WordPress Plugin from a scratch] (9AUG07) | |||
* [http://www.devlounge.net/articles/using-ajax-with-your-wordpress-plugin Using AJAX with your WordPress Plugin], also at DevLounce.net (25MAY07) | |||
* [http://asymptomatic.net/2005/02/22/1328/how-to-write-a-simple-wordpress-plugin/ "How to Write a Simple WordPress Plugin" at asymptomatic.net] (22FEB05) | |||
= 外部資源 = | = 外部資源 = |
請(qǐng)注意,您對(duì)站長百科的所有貢獻(xiàn)都可能被其他貢獻(xiàn)者編輯,修改或刪除。如果您不希望您的文字被任意修改和再散布,請(qǐng)不要提交。
您同時(shí)也要向我們保證您所提交的內(nèi)容是您自己所作,或得自一個(gè)不受版權(quán)保護(hù)或相似自由的來源(參閱Wordpress-mediawiki:版權(quán)的細(xì)節(jié))。 未經(jīng)許可,請(qǐng)勿提交受版權(quán)保護(hù)的作品!
您同時(shí)也要向我們保證您所提交的內(nèi)容是您自己所作,或得自一個(gè)不受版權(quán)保護(hù)或相似自由的來源(參閱Wordpress-mediawiki:版權(quán)的細(xì)節(jié))。 未經(jīng)許可,請(qǐng)勿提交受版權(quán)保護(hù)的作品!
編輯幫助(在新窗口中打開)
本頁使用的模板:
- 模板:WordPress導(dǎo)航(查看源代碼)(受保護(hù))