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

WordPress:Writing a Plugin

來自站長百科
Seadragon530討論 | 貢獻(xiàn)2008年5月11日 (日) 18:35的版本
跳轉(zhuǎn)至: 導(dǎo)航、? 搜索

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 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.

介紹

WordPress 1.2版本之前,如果你想要修改WordPress行為,你必須編輯(或者 "hack") WordPress 源代碼。盡管如此,在更多的現(xiàn)行WordPress版本中 ,你可以通過使用插件很容易的修改添加核心功能?;镜牟寮Y(jié)構(gòu)的想法是保持WordPress核心相關(guān)內(nèi)容的簡潔,但是富有靈活性,足以使各個輸入輸出方面可以通過插件來修改。下面是定義:

WordPress 插件
WordPress 插件是一個程序,或者是一組或更多函數(shù),由PHP腳本語言編寫,添加某種特殊的功能或者服務(wù)到WordPress weblog,使用接入點和WordPress Plugin Application Program Interface (API) 提供的方法,可以與weblog很好的結(jié)合在一起。

如果你發(fā)現(xiàn)你希望WordPress能擁有一些新的或者改良的功能,第一件事就是搜索各種插件庫(你可以從 插件文章中了解),然后看是否某人已經(jīng)創(chuàng)建了這樣的適合你的需要的插件。如果沒有,本文將指導(dǎo)你如何創(chuàng)建你自己的插件的過程。

本文假設(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 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ā)者的的全面的文章列表和資源,插件資源中包括有關(guān)編寫插件的外部文章以及關(guān)于特比話題的文章。
  • 另外一個了解有關(guān)插件的好方法是看寫的好的插件的PHP 源代碼,如 Hello Dolly, 一個分布式插件。
  • 一旦你寫了插件,閱讀插件提交和宣傳來學(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)建插件

本部分進(jìn)入你需要遵守的幾個步驟,和一些考慮什么時候建立一個結(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.

名字,文件和位置

插件名字

創(chuàng)建插件的第一個任務(wù)是想出插件要做什么,然后起一個(希望是唯一的)名字。 查看插件 和其他的相關(guān)庫,驗證你的名字是唯一的;你也可以使用Google搜索。 多數(shù)插件開發(fā)者選擇使用能描述插件功能的那個名字; 如,一個聯(lián)系到天氣的插件啃含有單詞"weather"在名字中。名字可以是好幾個詞。

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 fabfunc.php. 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, wp-content/plugins/, so no two plugins they are using can have the same PHP file name.

插件文件

下一步是創(chuàng)建PHP文件,文件名由你選擇的插件名衍生而來。如,如果你的插件叫做"Fabulous Functionality",你可以吧你的PHP文件起名為 fabfunc.php。試著取一個唯一的名字。人們安裝你的插件的時候會把這個PHP文件放到WordPress插件目錄中,wp-content/plugins/,所以沒有兩個不同的在使用的插件擁有相同的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 fabfunc and fabfunc.php in this example, put all your plugin's files into that directory, and tell your plugin users to install the whole directory under wp-content/plugins/.

In the rest of this article, "the plugin PHP file" refers to the main plugin PHP file, whether in wp-content/plugins/ or a sub-directory.

另外一個操作是把你的插件分成幾個文件,你的插件至少要有一個PHP 文件,它可以包含JavaScript文件,CSS文件,圖片文件和語言文件等等。如果有幾種文件選擇了唯一的文件夾和主PHP文件名字,如fabfuncfabfunc.php,就把所有的插件文件放到這個目錄下,然后告訴你的插件用戶安裝整個目錄到wp-content/plugins/下。

在這個文章的其余部分,"插件PHP文件"引用了主插件PHP文件,可以在 wp-content/plugins/ 或者一個子目錄下

Readme File

If you want to host your plugin on http://wordpress.org/extend/plugins/, you also need to create a readme.txt 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/上建立一個插件主機(jī),你還需要用標(biāo)準(zhǔn)格式創(chuàng)建readme.txt文件,把它放到你的插件中去。參見 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)建一個網(wǎng)頁來作為你的插件的主頁也是非常有用的。這個頁面可以描述如何安裝插件,它可以做些什么,兼容WordPress 的哪個版本,版本更替的時候你的插件做出了哪些改動,如何使用插件等等。

File Headers

Now it's time to put some information into your main plugin PHP file.

文件標(biāo)題

現(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:

<?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
*/
?>

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)插件信息

插件主PHP文件的頂部必須包含一個標(biāo)準(zhǔn)插件信息標(biāo)題。這個標(biāo)題讓W(xué)ordPress 識別你的插件存在,添加到插件管理界面以便激活,載入,運(yùn)行; 沒有標(biāo)題,你的插件將不會被激活也不會運(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需要用來識別你的插件的最小信息是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 GPL license used by WordPress or a license compatible with the GPL. To indicate a GPL license, include the following lines in your plugin:

<?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
*/
?>

許可

根據(jù)帶有許可信息這個標(biāo)準(zhǔn)標(biāo)題是個慣例。多數(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
*/
?>

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ī)劃你的插件

現(xiàn)在是該讓你的插件做些什么的時候了。這個部分包含了一些一般的關(guān)于插件開發(fā)的思想,描述了如何完成你的插件需要完成的幾個任務(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"來完成它們的目標(biāo)。 插件hooks的工作方式是在WordPress運(yùn)行的時候,在各個不同的時候,WordPress檢查是否有插件在那時有注冊過的函數(shù)在此刻運(yùn)行,如果有的話,函數(shù)運(yùn)行。三個函數(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)題到瀏覽器輸出的時候,首先檢查是否有任何插件為"filter"hook注冊了一個函數(shù)叫做"the_title"。如果有,標(biāo)題文字會被每個注冊函數(shù)按順序傳遞,最終結(jié)果就是打印出來的結(jié)果。這樣,如果你的插件需要添加一些信息到打印標(biāo)題,它可以注冊一個"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.

另一個例子是"action" hook,叫做"wp_footer"。在WordPress生成的HTML頁面的底部,它檢查看是否某個插件已經(jīng)為 "wp_footer" action hook注冊函數(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注冊函數(shù),什么插件hooks在WordPress和在插件API中可用。如果你在WordPress代碼中發(fā)現(xiàn)一個你想使用action或者filter的地方,但是WordPress沒有,你可以建議使用新的hooks (建議通常都會被采納); 如何建議參見提交錯誤。

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 geotag_list_states() 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 <?php and ?>.

模板標(biāo)簽

另外一種用插件添加WordPress功能的方法是通過建立自定義 模板標(biāo)簽。有人想使用你的插件添加這些 "標(biāo)簽"到他們的主題,邊欄,文章目錄或者其他任何合適的地方。例如,添加地理標(biāo)簽到文章中的插件,可以為邊欄定義一個叫做geotag_list_states()的模板標(biāo)簽函數(shù),列出所有的有關(guān)國家,并帶有國家文章頁面的鏈接。

想要定義一個模板標(biāo)簽,需要簡單的寫一個PHP 函數(shù)并為插件用戶在插件主頁或者是插件PHP主文件中寫下資料說明。在給函數(shù)做資料說明時,給出一個具體需要加入什么到主題文件中以使用函數(shù)的例子是個不錯的想法包括 <?php?>.

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:

  1. 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.
  2. 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ù)插件需要從站長或者blog用戶得到一些信息并保存在sessions中,為了在它的filter 函數(shù), action 函數(shù)和模板函數(shù)中使用。這個信息必須保存在WordPress 數(shù)據(jù)庫中,為了在sessions中可以持久穩(wěn)定。有兩種基本方法可以保存插件數(shù)據(jù)到數(shù)據(jù)庫中:

  1. 使用WordPress "操作"機(jī)制(下有描述)。這個方法適合儲存相關(guān)的小量靜態(tài)的指定的數(shù)據(jù) – 那種你希望在第一次設(shè)置插件時站長輸入的,今后幾乎不會更改的數(shù)據(jù)。
  2. 創(chuàng)建一個新的自定義的數(shù)據(jù)庫表格。這種方法適合聯(lián)合單獨的文章,頁面,附件或者評論的數(shù)據(jù)—一種隨著時間而增加的,沒有單獨的名字的數(shù)據(jù)。參見 創(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.

add_option($name, $value, $description, $autoload);
Creates a new option; does nothing if option already exists.
$name
Required (string). Name of the option to be added.
$value
Optional (string), defaults to empty string. The option value to be stored.
$description
Optional (string), defaults to empty string. Description of the option, which is placed into the WordPress database in case someone browses the database to see what options are there.
$autoload
Optional, defaults to 'yes' (enum: 'yes' or 'no'). If set to 'yes' the setting is automatically retrieved by the get_alloptions function.
get_option($option);
Retrieves an option value from the database.
$option
Required (string). Name of the option whose value you want returned. You can find a list of the default options that are installed with WordPress at the WordPress:Option Reference.
update_option($option_name, $newvalue);
Updates or creates an option value in the database (note that add_option does not have to be called if you do not want to use the $description or $autoload parameters).
$option_name
Required (string). Name of the option to update.
$newvalue
Required. The new value for the option.

WordPress 操作機(jī)制

參見創(chuàng)建操作頁面 得到關(guān)于如何創(chuàng)建自動為你保存操作的頁面的信息。

WordPress有一個保存,更新,重新得到個體, 制定的數(shù)據(jù)("操作")機(jī)制,在 WordPress 數(shù)據(jù)庫中。操作值可以是字符串,數(shù)組或者是PHP對象(它們將被 "連載",或者在存儲前轉(zhuǎn)變?yōu)樽址谥匦碌玫絺€體時反連載)。操作名字是字符串,它們必須是唯一的,這樣它們不會與其他 WordPress或者是插件起沖突。

這里是你的插件可以用來進(jìn)入WordPress操作的主要函數(shù)。

add_option($name, $value, $description, $autoload);
Creates a new option; does nothing if option already exists.
$name
Required (string). Name of the option to be added.
$value
Optional (string), defaults to empty string. The option value to be stored.
$description
Optional (string), defaults to empty string. Description of the option, which is placed into the WordPress database in case someone browses the database to see what options are there.
$autoload
Optional, defaults to 'yes' (enum: 'yes' or 'no'). If set to 'yes' the setting is automatically retrieved by the get_alloptions function.
get_option($option);
Retrieves an option value from the database.
$option
Required (string). Name of the option whose value you want returned. You can find a list of the default options that are installed with WordPress at the WordPress:Option Reference.
update_option($option_name, $newvalue);
Updates or creates an option value in the database (note that add_option does not have to be called if you do not want to use the $description or $autoload parameters).
$option_name
Required (string). Name of the option to update.
$newvalue
Required. The new value for the option.

Administration Panels

Assuming that your plugin has some options stored in the WordPress database (see section above), you will probably want it to have an administration panel that will enable your plugin users to view and edit option values. The methods for doing this are described in WordPress:Adding Administration Menus.

管理面板

假設(shè)你的插件有一些操作存儲在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.

讓你的插件國際化

一旦你插件的程序完成了,另一個需要考慮的是(假設(shè)你打算發(fā)布你的插件) 國際化. 國際化是建立軟件的一個過程,這樣它才可以本地化 ;本地化是翻譯軟件的顯示文本為其他語言。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 .php), and must be unique among plugins the user has installed.
  • Wherever your plugin uses literal text strings that will be displayed to the user (known as "messages"), wrap them in one of the two WordPress gettext functions. Note that in a plugin, you need to use the second argument, passing in the translation text domain name you chose, unlike in the core of WordPress (which leaves the $domain argument blank).
__($message, $domain)
Translates $message using the current locale for $domain. Wrap text strings that you are going to use in calculations with this function.
_e($message, $domain)
Translates $message using the current locale for $domain, and then prints it on the screen. Wrap text strings that you are directly printed with this function.

強(qiáng)力推薦你國際化你的插件,這樣來自不同國家的用戶,可以本地化插件,過程如下:

  • 給插件選擇一個翻譯"文本區(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以獲得關(guān)于POT文件,MO文件和本地文件的信息。
  • 通過在調(diào)用gettext函數(shù)之前調(diào)用load_plugin_textdomain為現(xiàn)有本地文件和你的文本區(qū)域文件載入翻譯,在session中越晚越好,(因為一些多語言插件載入時改變了本地文件)。一個可能的辦法是定義一個初始化函數(shù),在所有插件函數(shù)之前調(diào)用,如,假設(shè)你的文本區(qū)域是"fabfunc":
$fabfunc_domain = 'fabfunc';
$fabfunc_is_setup = 0;

function fabfunc_setup()
{
   global $fabfunc_domain, $fabfunc_is_setup;

   if($fabfunc_is_setup) {
      return;
   } 

   load_plugin_textdomain($fabfunc_domain, 'wp-content/plugins');
}

If your plugin is in its own subdirectory, append that to the second argument of load_plugin_textdomain

If you are reading this section because you want to internationalize a Theme, you can basically follow the steps above, except:

  • The MO file goes into the theme directory (same place as style.css).
  • The MO file is named ll_CC.mo, where ll_CC is the name of the locale (i.e. the domain is NOT part of the file name).
  • To load the text domain, put the following (inside a PHP escape if necessary) in your theme's functions.php file:
load_theme_textdomain('your_domain');

如果你閱讀了這個部分因為你想要國際化一個主題,你大體上可以按照上面的步驟來,除非:

  • 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ā)建議

最后一部分包括一些關(guān)于插件開發(fā)的建議。

  • 插件代碼應(yīng)該遵循 WordPress編碼標(biāo)準(zhǔn)。請參照內(nèi)嵌文檔標(biāo)準(zhǔn)。
  • 所有的插件中的函數(shù)需要有唯一的名字,因為這個原因,在所有插件函數(shù)中使用唯一的函數(shù)名字前綴是個很好的主意。另一個可能性是在類中定義你的插件函數(shù)(也需要有唯一名字).
  • 不要吧WordPress數(shù)據(jù)庫表格前綴(通常是"wp_")放入你的插件中。確認(rèn)使用不同的$wpdb->前綴來代替。
  • 數(shù)據(jù)庫閱讀很簡單但是寫確很難,數(shù)據(jù)庫是非常善于取數(shù)據(jù)給你,而且這些操作(通常是)如閃電般迅速。在數(shù)據(jù)庫中做改動,卻是一個復(fù)雜的多的過程,很難。所以試著最小化寫入的數(shù)量。首先在你的代碼中準(zhǔn)備好一切,這樣你只需要選擇那些你需要的寫操作。
  • 只選擇你需要的。盡管數(shù)據(jù)庫取數(shù)據(jù)非常快,你需要試著減少數(shù)據(jù)庫的負(fù)載,通過只選擇你需要的數(shù)據(jù)。如果你需要知道一個表格有多少行,不要SELECT * FROM,因為所有行中的所有數(shù)據(jù)都會被牽扯到,浪費(fèi)內(nèi)存。這樣,如果你只需要插件中的post_id 和post_author,只要SELECT那些特定的區(qū)域,來減小數(shù)據(jù)庫負(fù)載。記住: 大量的其他進(jìn)程可能同時需要使用數(shù)據(jù)庫。數(shù)據(jù)庫和服務(wù)器每個都有大量的資源供這些進(jìn)程使用。了解如何減小你的插件需要的資源會保證你的插件不會是被人說成濫用資源的哪種。


External Resources

外部資源