编辑“WordPress:Writing a Plugin

跳转至: 导航、​ 搜索
警告:您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您登录创建一个账户,您的编辑将归属于您的用户名,且将享受其他好处。

该编辑可以被撤销。 请检查下面的对比以核实您想要撤销的内容,然后发布下面的更改以完成撤销。

最后版本 您的文本
第1行: 第1行:
<span style="border:1px solid #000; text-align:center; float:right; padding:6px;"><strong>导航:</strong> [[WordPress:WordPress文档|上一级]] | [[WordPress]] | {{Template:WordPress导航}}</span>
<div style="clear:both;"></div>
= 介绍 =
= 介绍 =


第150行: 第148行:


一旦你插件的程序完成了,另一个需要考虑的是(假设你打算发布你的插件) ''国际化''. 国际化是建立软件的一个过程,这样它才可以''本地化'' ;本地化是翻译软件的显示文本为其他语言。WordPress是全世界使用的,所以它有国际化和本地化版本,包括本地化的插件。想要知道关于WordPress的 GNU gettext的本地化使用,参见[[WordPress:Translating WordPress]].  
一旦你插件的程序完成了,另一个需要考虑的是(假设你打算发布你的插件) ''国际化''. 国际化是建立软件的一个过程,这样它才可以''本地化'' ;本地化是翻译软件的显示文本为其他语言。WordPress是全世界使用的,所以它有国际化和本地化版本,包括本地化的插件。想要知道关于WordPress的 GNU gettext的本地化使用,参见[[WordPress:Translating WordPress]].  
It is highly recommended that you internationalize your plugin, so that users from different countries can localize it. The process is fairly straightforward:
* Choose a translation "text domain" name for your plugin. This is generally the same as your plugin file name (without the <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.


强力推荐你国际化你的插件,这样来自不同国家的用户,可以本地化插件,过程如下:
强力推荐你国际化你的插件,这样来自不同国家的用户,可以本地化插件,过程如下:
第155行: 第159行:
* 不论在哪里插件使用的显示给用户的文本(即"消息"), 使用一个或者两个 WordPress gettext 函数把它们包围起来。注意在插件中,你需要使用第二种方法,你选择的翻译文本区域名字中的传递,不像WordPress核心(留下 <tt>$domain</tt> 这部分空白).
* 不论在哪里插件使用的显示给用户的文本(即"消息"), 使用一个或者两个 WordPress gettext 函数把它们包围起来。注意在插件中,你需要使用第二种方法,你选择的翻译文本区域名字中的传递,不像WordPress核心(留下 <tt>$domain</tt> 这部分空白).
; <tt>__($message, $domain)</tt> :使用现有本地<tt>$domain</tt>翻译 <tt>$message</tt>。换行你将要用来用这个函数计算的文字字符串。
; <tt>__($message, $domain)</tt> :使用现有本地<tt>$domain</tt>翻译 <tt>$message</tt>。换行你将要用来用这个函数计算的文字字符串。
; <tt>_e($message, $domain)</tt> : 使用现有本地<tt>$domain</tt>翻译 <tt>$message</tt>, 然后在屏幕上显示出来,给你使用这个函数直接显示出的文本字符串换行。
; <tt>_e($message, $domain)</tt> : 使用现有本地<tt>$domain</tt>翻译 <tt>$message</tt>, 然后在屏幕上显示出来,换行你使用这个函数直接显示出的文本字符串。
 
* 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>.


* 创建一个POT文件(翻译目录列出了所有的可以翻译的信息),随着你的插件分发出去。用户将需要把他们的翻译过的MO文件放在你的插件的PHP文件的同一目录下,命名为<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。参见[[WordPress:Translating WordPress|翻译WordPress]]以获得关于POT文件,MO文件和本地文件的信息。
* 创建一个POT文件(翻译目录列出了所有的可以翻译的信息),随着你的插件分发出去。用户将需要把他们的翻译过的MO文件放在你的插件的PHP文件的同一目录下,命名为<tt>domain-ll_CC.mo</tt>,那里<tt>ll_CC</tt>是本地文件的名字。参见[[WordPress:Translating WordPress|翻译WordPress]]以获得关于POT文件,MO文件和本地文件的信息。
第176行: 第198行:
</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>


如果你阅读了这个部分因为你想要国际化一个主题,你大体上可以按照上面的步骤来,除非:
如果你阅读了这个部分因为你想要国际化一个主题,你大体上可以按照上面的步骤来,除非:
第184行: 第214行:
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.


= 插件开发建议 =
= 插件开发建议 =
第191行: 第231行:
* 插件代码应该遵循[[WordPress:WordPress Coding Standards| WordPress编码标准]]。请参照[[WordPress:Inline Documentation|内嵌文档]]标准。
* 插件代码应该遵循[[WordPress:WordPress Coding Standards| WordPress编码标准]]。请参照[[WordPress:Inline Documentation|内嵌文档]]标准。
* 所有的插件中的函数需要有唯一的名字,因为这个原因,在所有插件函数中使用唯一的函数名字前缀是个很好的主意。另一个可能性是在类中定义你的插件函数(也需要有唯一名字).
* 所有的插件中的函数需要有唯一的名字,因为这个原因,在所有插件函数中使用唯一的函数名字前缀是个很好的主意。另一个可能性是在类中定义你的插件函数(也需要有唯一名字).
* 不要把WordPress数据库表格前缀(通常是"wp_")放入你的插件中。确认使用不同的<tt>$wpdb->前缀</tt>来代替。  
* 不要吧WordPress数据库表格前缀(通常是"wp_")放入你的插件中。确认使用不同的<tt>$wpdb->前缀</tt>来代替。  
* 数据库阅读很简单但是写确很难,数据库是非常善于取数据给你,而且这些操作(通常是)如闪电般迅速。在数据库中做改动,却是一个复杂的多的过程,很难。所以试着最小化<em>写入</em>的数量。首先在你的代码中准备好一切,这样你只需要选择那些你需要的写操作。
* 数据库阅读很简单但是写确很难,数据库是非常善于取数据给你,而且这些操作(通常是)如闪电般迅速。在数据库中做改动,却是一个复杂的多的过程,很难。所以试着最小化<em>写入</em>的数量。首先在你的代码中准备好一切,这样你只需要选择那些你需要的写操作。
* 只选择你需要的。尽管数据库取数据非常快,你需要试着减少数据库的负载,通过只选择你需要的数据。如果你需要知道一个表格有多少行,不要<tt>SELECT * FROM</tt>,因为所有行中的所有数据都会被牵扯到,浪费内存。这样,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的区域,来减小数据库负载。记住: 大量的其他进程可能同时需要使用数据库。数据库和服务器每个都有大量的资源供这些进程使用。了解如何减小你的插件需要的资源会保证你的插件不会是被人说成滥用资源的哪种。
* 只选择你需要的。尽管数据库取数据非常快,你需要试着减少数据库的负载,通过只选择你需要的数据。如果你需要知道一个表格有多少行,不要<tt>SELECT * FROM</tt>,因为所有行中的所有数据都会被牵扯到,浪费内存。这样,如果你只需要插件中的post_id 和post_author,只要<tt>SELECT</tt>那些特定的区域,来减小数据库负载。记住: 大量的其他进程可能同时需要使用数据库。数据库和服务器每个都有大量的资源供这些进程使用。了解如何减小你的插件需要的资源会保证你的插件不会是被人说成滥用资源的哪种。


= 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)


= 外部资源 =
= 外部资源 =
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅Wordpress-mediawiki:版权的细节)。 未经许可,请勿提交受版权保护的作品!
取消 编辑帮助(在新窗口中打开)

本页使用的模板: