久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

WordPress: Writing a Plugin:修訂間差異

來(lái)自站長(zhǎng)百科
跳轉(zhuǎn)至: 導(dǎo)航、? 搜索
無(wú)編輯摘要
無(wú)編輯摘要
?
(未顯示2個(gè)用戶的5個(gè)中間版本)
第1行: 第1行:
= Introduction =
<span style="border:1px solid #000; text-align:center; float:right; padding:6px;"><strong>導(dǎo)航:</strong> [[WordPress:WordPress文檔|上一級(jí)]] | [[WordPress]] | {{Template:WordPress導(dǎo)航}}</span>
?
<div style="clear:both;"></div>
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.
?
= 介紹 =
= 介紹 =


第18行: 第10行:


本文假設(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.
== 資源 ==
== 資源 ==


* 有很多插件開(kāi)發(fā)者的的全面的文章列表和資源,[[WordPress:Plugin Resources|插件資源]]中包括有關(guān)編寫(xiě)插件的外部文章以及關(guān)于特比話題的文章。
* 有很多插件開(kāi)發(fā)者的的全面的文章列表和資源,[[WordPress:Plugin Resources|插件資源]]中包括有關(guān)編寫(xiě)插件的外部文章以及關(guān)于特別話題的文章。
* 另外一個(gè)了解有關(guān)插件的好方法是看寫(xiě)的好的插件的PHP 源代碼,如 [[WordPress:Plugins#Default Plugins|Hello Dolly]], 一個(gè)分布式插件。 ?
* 另外一個(gè)了解有關(guān)插件的好方法是看寫(xiě)的好的插件的PHP 源代碼,如 [[WordPress:Plugins#Default Plugins|Hello Dolly]], 一個(gè)分布式插件。 ?
* 一旦你寫(xiě)了插件,閱讀[[WordPress:Plugin Submission and Promotion|插件提交和宣傳]]來(lái)學(xué)習(xí)如何讓它更廣泛的散布開(kāi)來(lái)。
* 一旦你寫(xiě)了插件,閱讀[[WordPress:Plugin Submission and Promotion|插件提交和宣傳]]來(lái)學(xué)習(xí)如何讓它更廣泛的散布開(kāi)來(lái)。
= 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.


== 名字,文件和位置 ==
== 名字,文件和位置 ==
第49行: 第24行:
=== 插件名字 ===
=== 插件名字 ===


創(chuàng)建插件的第一個(gè)任務(wù)是想出插件要做什么,然后起一個(gè)(希望是唯一的)名字。 查看[[WordPress:Plugins|插件]] 和其他的相關(guān)庫(kù),驗(yàn)證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開(kāi)發(fā)者選擇使用能描述插件功能的那個(gè)名字; 如,一個(gè)聯(lián)系到天氣的插件啃含有單詞"weather"在名字中。名字可以是好幾個(gè)詞。
創(chuàng)建插件的第一個(gè)任務(wù)是想出插件要做什么,然后起一個(gè)(希望是唯一的)名字。 查看[[WordPress:Plugins|插件]] 和其他的相關(guān)庫(kù),驗(yàn)證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開(kāi)發(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文件,文件名由你選擇的插件名衍生而來(lái)。如,如果你的插件叫做"Fabulous Functionality",你可以吧你的PHP文件起名為 <tt>fabfunc.php</tt>。試著取一個(gè)唯一的名字。人們安裝你的插件的時(shí)候會(huì)把這個(gè)PHP文件放到WordPress插件目錄中,<tt>wp-content/plugins/</tt>,所以沒(méi)有兩個(gè)不同的在使用的插件擁有相同的PHP文件名。 ?
下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來(lái)。如,如果你的插件叫做"Fabulous Functionality",你可以把你的PHP文件起名為 <tt>fabfunc.php</tt>。試著取一個(gè)唯一的名字。人們安裝你的插件的時(shí)候會(huì)把這個(gè)PHP文件放到WordPress插件目錄中,<tt>wp-content/plugins/</tt>,所以沒(méi)有兩個(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文件,圖片文件和語(yǔ)言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如<tt>fabfunc</tt> 和 <tt>fabfunc.php</tt>,就把所有的插件文件放到這個(gè)目錄下,然后告訴你的插件用戶安裝整個(gè)目錄到<tt>wp-content/plugins/</tt>下。
另外一個(gè)操作是把你的插件分成幾個(gè)文件,你的插件至少要有一個(gè)PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語(yǔ)言文件等等。如果有幾種文件選擇了唯一的文件夾和主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>文件,把它放到你的插件中去。參見(jiàn) 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>文件,把它放到你的插件中去。參見(jiàn) 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.


=== 主頁(yè) ===
=== 主頁(yè) ===


創(chuàng)建一個(gè)網(wǎng)頁(yè)來(lái)作為你的插件的主頁(yè)也是非常有用的。這個(gè)頁(yè)面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個(gè)版本,版本更替的時(shí)候你的插件做出了哪些改動(dòng),如何使用插件等等。
創(chuàng)建一個(gè)網(wǎng)頁(yè)來(lái)作為你的插件的主頁(yè)也是非常有用的。這個(gè)頁(yè)面可以描述如何安裝插件,它可以做些什么,兼容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)在開(kāi)始在你的插件的主PHP文件中添加信息。
現(xiàn)在開(kāi)始在你的插件的主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)插件信息 ===
第128行: 第64行:


WordPress需要用來(lái)識(shí)別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來(lái)創(chuàng)建插件管理界面上的插件表格。這些行的順序并不重要。
WordPress需要用來(lái)識(shí)別你的插件的最小信息是Plugin Name這行,其他信息(如果出現(xiàn)的話) 將用來(lái)創(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>


=== 許可 ===
=== 許可 ===
第178行: 第89行:
?>
?>
</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)于插件開(kāi)發(fā)的思想,描述了如何完成你的插件需要完成的幾個(gè)任務(wù)。
現(xiàn)在是該讓你的插件做些什么的時(shí)候了。這個(gè)部分包含了一些一般的關(guān)于插件開(kāi)發(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 ===


很多插件通過(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)行為。
很多插件通過(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)行為。
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é)果就是打印出來(lái)的結(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é)果就是打印出來(lái)的結(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頁(yè)面的底部,它檢查看是否某個(gè)插件已經(jīng)為 "wp_footer" action hook注冊(cè)函數(shù),然后依次運(yùn)行。
另一個(gè)例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁(yè)面的底部,它檢查看是否某個(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沒(méi)有,你可以建議使用新的hooks (建議通常都會(huì)被采納); 如何建議參見(jiàn)[[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沒(méi)有,你可以建議使用新的hooks (建議通常都會(huì)被采納); 如何建議參見(jiàn)[[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功能的方法是通過(guò)建立自定義 [[WordPress:Template Tags|模板標(biāo)簽]]。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個(gè)叫做<tt>geotag_list_states()</tt>的模板標(biāo)簽函數(shù),列出所有的有關(guān)國(guó)家,并帶有國(guó)家文章頁(yè)面的鏈接。
另外一種用插件添加WordPress功能的方法是通過(guò)建立自定義 [[WordPress:Template Tags|模板標(biāo)簽]]。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個(gè)叫做<tt>geotag_list_states()</tt>的模板標(biāo)簽函數(shù),列出所有的有關(guān)國(guó)家,并帶有國(guó)家歸檔頁(yè)面的鏈接。


想要定義一個(gè)模板標(biāo)簽,需要簡(jiǎn)單的寫(xiě)一個(gè)PHP 函數(shù)并為插件用戶在插件主頁(yè)或者是插件PHP主文件中寫(xiě)下資料說(shuō)明。在給函數(shù)做資料說(shuō)明時(shí),給出一個(gè)具體需要加入什么到主題文件中以使用函數(shù)的例子是個(gè)不錯(cuò)的想法包括 <tt><?php</tt> 和 <tt>?></tt>.
想要定義一個(gè)模板標(biāo)簽,需要簡(jiǎn)單的寫(xiě)一個(gè)PHP 函數(shù)并為插件用戶在插件主頁(yè)或者是插件PHP主文件中寫(xiě)下資料說(shuō)明。在給函數(shù)做資料說(shuō)明時(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ù)庫(kù)===
===保持插件數(shù)據(jù)到數(shù)據(jù)庫(kù)===
第232行: 第115行:
# 使用WordPress "操作"機(jī)制(下有描述)。這個(gè)方法適合儲(chǔ)存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時(shí)站長(zhǎng)輸入的,今后幾乎不會(huì)更改的數(shù)據(jù)。
# 使用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) [[WordPress:Creating Tables with Plugins|創(chuàng)建插件表格]].
# 創(chuàng)建一個(gè)新的自定義的數(shù)據(jù)庫(kù)表格。這種方法適合聯(lián)合單獨(dú)的文章,頁(yè)面,附件或者評(píng)論的數(shù)據(jù)—一種隨著時(shí)間而增加的,沒(méi)有單獨(dú)的名字的數(shù)據(jù)。參見(jiàn) [[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ī)制===
第274行: 第127行:
add_option($name, $value, $description, $autoload);
add_option($name, $value, $description, $autoload);
</pre>
</pre>
: Creates a new option; does nothing if option already exists.
: 創(chuàng)建一個(gè)新選項(xiàng);如果選項(xiàng)存在就什么都不做
;$name: Required (string). Name of the option to be added.
;$name: Required (string). 將被添加的選項(xiàng)名字
;$value: Optional (string), defaults to empty string. The option value to be stored.
;$value: Optional (string), 默認(rèn)為空的字符串。 將被存儲(chǔ)的選項(xiàng)值
;$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.
;$description: Optional (string), 默認(rèn)為空的字符串。選項(xiàng)的描述,被放在wordpress數(shù)據(jù)庫(kù)中以防某些人瀏覽數(shù)據(jù)庫(kù)去看選項(xiàng)在哪。
;$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.
;$autoload: 選項(xiàng),默認(rèn)為'yes' (enum: 'yes' or 'no').如果設(shè)置為'yes',那么設(shè)置被<tt>get_alloptions</tt>函數(shù)自動(dòng)檢索。
?
<pre>
<pre>
get_option($option);
get_option($option);
</pre>
</pre>
: Retrieves an option value from the database.
: 從數(shù)據(jù)庫(kù)中檢索一個(gè)選項(xiàng)值
;$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]].
;$option: Required (string).? 你想要返回的選項(xiàng)值的選項(xiàng)的名稱(chēng)。你能夠找到被安裝在[[WordPress:Option Reference|相關(guān)選項(xiàng)]]中的一列默認(rèn)選項(xiàng)
?
<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).
: 在數(shù)據(jù)庫(kù)里更新或者創(chuàng)建一個(gè)選項(xiàng)值(注:如果你不想使用<tt>$description</tt> 或者 <tt>$autoload</tt>參數(shù),<tt>add_option</tt>就沒(méi)必要被訪問(wèn)。)
;$option_name: Required (string). Name of the option to update.
;$option_name: Required (string). 選項(xiàng)名稱(chēng)更新。
;$newvalue: Required.? The new value for the option.
;$newvalue: Required.? 為選項(xiàng)賦予新的值。
?
=== 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)查看和編輯操作。方法可以在 [[WordPress:Adding Administration Menus|添加管理目錄](méi)]中找到。
假設(shè)你的插件有一些操作存儲(chǔ)在WordPress數(shù)據(jù)庫(kù)中 (參見(jiàn)上面的部分),你也許希望有一個(gè)管理面板,允許你的插件用戶來(lái)查看和編輯操作。方法可以在 [[WordPress:Adding Administration Menus|添加管理目錄](mé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ó)際化 ==
== 讓你的插件國(guó)際化 ==


一旦你插件的程序完成了,另一個(gè)需要考慮的是(假設(shè)你打算發(fā)布你的插件) ''國(guó)際化''. 國(guó)際化是建立軟件的一個(gè)過(guò)程,這樣它才可以''本地化'' ;本地化是翻譯軟件的顯示文本為其他語(yǔ)言。WordPress是全世界使用的,所以它有國(guó)際化和本地化版本,包括本地化的插件。想要知道關(guān)于WordPress的 GNU gettext的本地化使用,參見(jiàn)[[WordPress:Translating WordPress]]. ?
一旦你插件的程序完成了,另一個(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 <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)力推薦你國(guó)際化你的插件,這樣來(lái)自不同國(guó)家的用戶,可以本地化插件,過(guò)程如下:
強(qiáng)力推薦你國(guó)際化你的插件,這樣來(lái)自不同國(guó)家的用戶,可以本地化插件,過(guò)程如下:
第319行: 第155行:
* 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個(gè)或者兩個(gè) WordPress gettext 函數(shù)把它們包圍起來(lái)。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 <tt>$domain</tt> 這部分空白).
* 不論在哪里插件使用的顯示給用戶的文本(即"消息"), 使用一個(gè)或者兩個(gè) WordPress gettext 函數(shù)把它們包圍起來(lái)。注意在插件中,你需要使用第二種方法,你選擇的翻譯文本區(qū)域名字中的傳遞,不像WordPress核心(留下 <tt>$domain</tt> 這部分空白).
; <tt>__($message, $domain)</tt> :使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>。換行你將要用來(lái)用這個(gè)函數(shù)計(jì)算的文字字符串。
; <tt>__($message, $domain)</tt> :使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>。換行你將要用來(lái)用這個(gè)函數(shù)計(jì)算的文字字符串。
; <tt>_e($message, $domain)</tt> : 使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>, 然后在屏幕上顯示出來(lái),換行你使用這個(gè)函數(shù)直接顯示出的文本字符串。
; <tt>_e($message, $domain)</tt> : 使用現(xiàn)有本地<tt>$domain</tt>翻譯 <tt>$message</tt>, 然后在屏幕上顯示出來(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 <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ā)出去。用戶將需要把他們的翻譯過(guò)的MO文件放在你的插件的PHP文件的同一目錄下,命名為<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。參見(jiàn)[[WordPress:Translating WordPress|翻譯WordPress]]以獲得關(guān)于POT文件,MO文件和本地文件的信息。
* 創(chuàng)建一個(gè)POT文件(翻譯目錄列出了所有的可以翻譯的信息),隨著你的插件分發(fā)出去。用戶將需要把他們的翻譯過(guò)的MO文件放在你的插件的PHP文件的同一目錄下,命名為<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。參見(jiàn)[[WordPress:Translating WordPress|翻譯WordPress]]以獲得關(guān)于POT文件,MO文件和本地文件的信息。
第358行: 第176行:
</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)槟阆胍獓?guó)際化一個(gè)主題,你大體上可以按照上面的步驟來(lái),除非:
如果你閱讀了這個(gè)部分因?yàn)槟阆胍獓?guó)際化一個(gè)主題,你大體上可以按照上面的步驟來(lái),除非:
第374行: 第184行:
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.


= 插件開(kāi)發(fā)建議 =
= 插件開(kāi)發(fā)建議 =
第391行: 第191行:
* 插件代碼應(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è)可能性是在類(lèi)中定義你的插件函數(shù)(也需要有唯一名字).
* 所有的插件中的函數(shù)需要有唯一的名字,因?yàn)檫@個(gè)原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個(gè)很好的主意。另一個(gè)可能性是在類(lèi)中定義你的插件函數(shù)(也需要有唯一名字).
* 不要吧WordPress數(shù)據(jù)庫(kù)表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的<tt>$wpdb->前綴</tt>來(lái)代替。 ?
* 不要把WordPress數(shù)據(jù)庫(kù)表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的<tt>$wpdb->前綴</tt>來(lái)代替。 ?
* 數(shù)據(jù)庫(kù)閱讀很簡(jiǎn)單但是寫(xiě)確很難,數(shù)據(jù)庫(kù)是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫(kù)中做改動(dòng),卻是一個(gè)復(fù)雜的多的過(guò)程,很難。所以試著最小化<em>寫(xiě)入</em>的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫(xiě)操作。
* 數(shù)據(jù)庫(kù)閱讀很簡(jiǎn)單但是寫(xiě)確很難,數(shù)據(jù)庫(kù)是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫(kù)中做改動(dòng),卻是一個(gè)復(fù)雜的多的過(guò)程,很難。所以試著最小化<em>寫(xiě)入</em>的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫(xiě)操作。
* 只選擇你需要的。盡管數(shù)據(jù)庫(kù)取數(shù)據(jù)非???,你需要試著減少數(shù)據(jù)庫(kù)的負(fù)載,通過(guò)只選擇你需要的數(shù)據(jù)。如果你需要知道一個(gè)表格有多少行,不要<tt>SELECT * FROM</tt>,因?yàn)樗行兄械乃袛?shù)據(jù)都會(huì)被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的區(qū)域,來(lái)減小數(shù)據(jù)庫(kù)負(fù)載。記住: 大量的其他進(jìn)程可能同時(shí)需要使用數(shù)據(jù)庫(kù)。數(shù)據(jù)庫(kù)和服務(wù)器每個(gè)都有大量的資源供這些進(jìn)程使用。了解如何減小你的插件需要的資源會(huì)保證你的插件不會(huì)是被人說(shuō)成濫用資源的哪種。
* 只選擇你需要的。盡管數(shù)據(jù)庫(kù)取數(shù)據(jù)非??欤阈枰囍鴾p少數(shù)據(jù)庫(kù)的負(fù)載,通過(guò)只選擇你需要的數(shù)據(jù)。如果你需要知道一個(gè)表格有多少行,不要<tt>SELECT * FROM</tt>,因?yàn)樗行兄械乃袛?shù)據(jù)都會(huì)被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的區(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 =
* [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)


= 外部資源 =
= 外部資源 =

2008年5月27日 (二) 11:01的最新版本

導(dǎo)航: 上一級(jí) | WordPress | 首頁(yè) | WordPress中文論壇 | WordPress主機(jī) | CMS程序 | 論壇程序 | ECShop | ShopNC | PowerEasy

介紹[ ]

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ǔ)言編寫(xiě),添加某種特殊的功能或者服務(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)編寫(xiě)插件的外部文章以及關(guān)于特別話題的文章。
  • 另外一個(gè)了解有關(guān)插件的好方法是看寫(xiě)的好的插件的PHP 源代碼,如 Hello Dolly, 一個(gè)分布式插件。
  • 一旦你寫(xiě)了插件,閱讀插件提交和宣傳來(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文件名字,如fabfuncfabfunc.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)單的寫(xiě)一個(gè)PHP 函數(shù)并為插件用戶在插件主頁(yè)或者是插件PHP主文件中寫(xiě)下資料說(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ù)中:

  1. 使用WordPress "操作"機(jī)制(下有描述)。這個(gè)方法適合儲(chǔ)存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時(shí)站長(zhǎng)輸入的,今后幾乎不會(huì)更改的數(shù)據(jù)。
  2. 創(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);
創(chuàng)建一個(gè)新選項(xiàng);如果選項(xiàng)存在就什么都不做
$name
Required (string). 將被添加的選項(xiàng)名字
$value
Optional (string), 默認(rèn)為空的字符串。 將被存儲(chǔ)的選項(xiàng)值
$description
Optional (string), 默認(rèn)為空的字符串。選項(xiàng)的描述,被放在wordpress數(shù)據(jù)庫(kù)中以防某些人瀏覽數(shù)據(jù)庫(kù)去看選項(xiàng)在哪。
$autoload
選項(xiàng),默認(rèn)為'yes' (enum: 'yes' or 'no').如果設(shè)置為'yes',那么設(shè)置被get_alloptions函數(shù)自動(dòng)檢索。
get_option($option);
從數(shù)據(jù)庫(kù)中檢索一個(gè)選項(xiàng)值
$option
Required (string). 你想要返回的選項(xiàng)值的選項(xiàng)的名稱(chēng)。你能夠找到被安裝在相關(guān)選項(xiàng)中的一列默認(rèn)選項(xiàng)
update_option($option_name, $newvalue);
在數(shù)據(jù)庫(kù)里更新或者創(chuàng)建一個(gè)選項(xiàng)值(注:如果你不想使用$description 或者 $autoload參數(shù),add_option就沒(méi)必要被訪問(wèn)。)
$option_name
Required (string). 選項(xiàng)名稱(chēng)更新。
$newvalue
Required. 為選項(xiàng)賦予新的值。

管理面板[ ]

假設(shè)你的插件有一些操作存儲(chǔ)在WordPress數(shù)據(jù)庫(kù)中 (參見(jiàn)上面的部分),你也許希望有一個(gè)管理面板,允許你的插件用戶來(lái)查看和編輯操作。方法可以在 添加管理目錄中找到。

讓你的插件國(guó)際化[ ]

一旦你插件的程序完成了,另一個(gè)需要考慮的是(假設(shè)你打算發(fā)布你的插件) 國(guó)際化. 國(guó)際化是建立軟件的一個(gè)過(guò)程,這樣它才可以本地化 ;本地化是翻譯軟件的顯示文本為其他語(yǔ)言。WordPress是全世界使用的,所以它有國(guó)際化和本地化版本,包括本地化的插件。想要知道關(guān)于WordPress的 GNU gettext的本地化使用,參見(jiàn)WordPress:Translating WordPress.

強(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ù)直接顯示出的文本字符串換行。


  • 創(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

如果你閱讀了這個(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');

插件開(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è)可能性是在類(lèi)中定義你的插件函數(shù)(也需要有唯一名字).
  • 不要把WordPress數(shù)據(jù)庫(kù)表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的$wpdb->前綴來(lái)代替。
  • 數(shù)據(jù)庫(kù)閱讀很簡(jiǎn)單但是寫(xiě)確很難,數(shù)據(jù)庫(kù)是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫(kù)中做改動(dòng),卻是一個(gè)復(fù)雜的多的過(guò)程,很難。所以試著最小化寫(xiě)入的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫(xiě)操作。
  • 只選擇你需要的。盡管數(shù)據(jù)庫(kù)取數(shù)據(jù)非常快,你需要試著減少數(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ō)成濫用資源的哪種。


外部資源[ ]