Calendar

2010年三月
« 二    
1234567
891011121314
15161718192021
22232425262728
293031  

Translator

WordPress 插件\主题 本地化

一旦你的插件编程工作已结束,另一个值得考虑的方面就是怎样将你的插件国际化(当然,这是建立在你想把你的作品发布给公众的前提上)。“国际化”就是一个建设你的软件使之能被本地化的过程;而“本地化”就是将你软件显示文本翻译成各种语言的过程。WordPress 已被来自全世界各个地方的爱好者使用,所以它以将国际化和本地化功能融入其结构,包括插件程序的本地化功能。想了解更多的 GNU gettext 本地化的背景知识,请参照翻译 WordPress

极力推荐你将你的插件进行国际化,这样来自五湖四海的用户名都可以本地化它。而且这整个过程也是十分直接明了:

  • 先给你的插件选一个翻译用的“文本域”名称。这一般同你的插件文件名称相同,当然不包括.php后缀名,而且必须是具有唯一性,至少保证不要同使用者已安装的其他插件同名。
  • 不管任何时候你的插件要使用文字来显示给网页读者(也即“信息文”),都尽量将它们嵌入在如下两个 WordPress gettext 函数程序段的任一之中。记住,不同于 WordPress 核心代码,在你插件里的国际化函数里你应该使用第二个参数来传递你自选的文本域。而在 WordPress 核心代码,这个参数默认为空。

__() 用来将 message 作为一个参数传递给另外一个函数。 _e() 用来直接把 message 写到页面上。

__($message, $domain) : 用本地化的语言为 $domain 翻译 $message 。输出的字符串可以继续被其他函数调用。
_e($message, $domain) :用本地化的语言为 $domain 翻译 $message ,然后显示到用户的屏幕。如果你的文字将直接显示给读者,就可以使用该函数。
  • 为你的插件创建一个 POT 文件 (这个一个列有所有文字条的翻译条目文件),随你的插件一起发布。插件用户将需要生成一个翻译本地化好的 MO 文件,然后把它放在你插件的同一文件夹里。并且这个 mo 文件的取名也有讲究,应该是象 domain-ll_CC.mo,这里 ll_CC 就是本地国家和语言代码(如 zh_CN)。参照 翻译 WordPress 上更多关于 POT 文件、MO 文件及本地化的信息。
  • 在你插件里加入一个 load_plugin_textdomain 子程序用以调用你的插件翻译。这个子程序一定需要在你 gettext 函数的前面,但最好是越晚越好(因为一些多语言插件在调用时会改变一些地域设置)。一个可行的方法就是在你所有插件的子程序前面增加一个初始子程序。例如,假设你的文本域叫 “fabfunc”,则:

Click to continue reading

Rewrite Permalinks in plugin development.

We add a rewrite rule that tells WordPress to interpret /geostate/oregon URLs the same as ?geostate=oregon.
there are two steps:

(1) “flush” the cached rewrite rules using an init filter, to force WordPress to recalculate the rewrite rules,
add_action('init', 'geotags_flush_rewrite_rules');

function geotags_flush_rewrite_rules()
{
   global $wp_rewrite;
   $wp_rewrite->flush_rules();
}
(2) use the generate_rewrite_rules action to add a new rule when they are calculated. Here’s the “flush” code:
add_action('generate_rewrite_rules', 'geotags_add_rewrite_rules');

function geotags_add_rewrite_rules( $wp_rewrite )
{
  $new_rules = array(
     'geostate/(.+)' => 'index.php?geostate=' .
       $wp_rewrite->preg_index(1) );

  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

One guy WP-Codebox release suggestion

Hello –

Let me first tell you that I love this plugin. Today I had the brilliant idea of changing the ‘+’ and ‘-’ symbol that are in the upper right corner of the box. I wanted to use an image instead. At first I just changed the ‘main.php’ file. That made the images appear fine when the page loaded. Although, once clicked on, it would go back to the text version. I determined that the problem was with the javascript file. I found where I though should be changed and I got it to work fine in Firefox, but it’s getting stuck in Internet Explorer.

I’ve attached a text file of the changes that I’ve made. Is there any way that you can help me out with this?

Thank you so much!

Bill Fisher

Click to continue reading

wordpress shortCode API

wordpress2.5增加了一个新的简码(shortCode)API,是一个简单的函数集,用于创建发布内容中的宏代码。简码形式如下:[play] or [codebox]…[/codebox]

有了简码API,创建支持下列属性的简码就变得容易了。简码API能够处理所有形式的解析工作,不再需要为每个简码编写相应的正则表达式。Helper函数也包含进API了,用于设置和获取默认属性。对于自动关闭和被动关闭的简码,API都提供支持。

概述

简码是用来提供处理函数的。简码处理器在很大程序上类似于wordpress的过滤器filter:它们都接受参数(属性)并返回结果(简码输出)。函数add_shortcode()用于注册简码处理器。它有两个参数:简码名(用于文章本身的字符串)和处理器函数名。简码处理器函数应该接受一到两个参数:$atts(属性的数组)和$content(其中的内容)(如果简码用于包含其中的表单)。
例如:function my_shortcode_handler($atts, $content=null) {}用于注册简码处理器的API调用,
形式如下:add_shortcode(‘my-shortcode’, ‘my_shortcode_handler’);

当the_content显示的时候,简码API就会解析所有的简码,比如”[my-shortcode]“,如果有属性和内容,就会把它们分离并解析出来,然后传递给相应的简码处理器函数。任何由处理器返回的字符串都会被插入到文章本身,替换掉简码的位置。简码属性如下:[my-shortcode foo="bar" baz="bing"]code content……..[/my-shortcode]它们会被转化为关联数组(如下),作为$atts参数传递给处理器函数;内容作为$content传递。
array( ‘foo’ => ‘bar’, ‘baz’ => ‘bing’)关联数组的键是属性名,而值就是相应的属性值。

Click to continue reading

wpdb 创建wp数据表用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
global $wpdb;
$wpdb->donators = $wpdb->prefix.'donators'; //Donators Table Name
### Function: Create Donators Table
register_activation_hook(__FILE__,'install');
function install(){
	global $wpdb;
 
	if(@is_file(ABSPATH.'/wp-admin/upgrade-functions.php')) {
		include_once(ABSPATH.'/wp-admin/upgrade-functions.php');
	} elseif(@is_file(ABSPATH.'/wp-admin/includes/upgrade.php')) {
		include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
	} else {
		die('We have problem finding your \'/wp-admin/upgrade-functions.php\' and \'/wp-admin/includes/upgrade.php\'');
	}
 
	// Create Donators Table
	$create_table = "CREATE TABLE $wpdb->donators (".
			"ID bigint(20) NOT NULL auto_increment,".
			"BUYER_EMAIL varchar(100) NOT NULL default '',".
			"ITEMNAME varchar(255) NOT NULL default'',".
			"ITEMNUMBER varchar(50),".
			"URL varchar(255),".
			"USER_MEMO varchar(255),".
			"PAYMENTDATE varchar(50) NOT NULL default '',".
			"TXNID varchar(30) NOT NULL default '',".
			"PAYMENT_GROSS varchar(10) NOT NULL default '',".
			"PAYMENT_FEE varchar(10) NOT NULL default '',".
			"MC_CURRENCY varchar(5) NOT NULL default '',".
			"INVOICE varchar(255) NOT NULL default '',".
			"PRIMARY KEY (ID));";
	maybe_create_table($wpdb->donators, $create_table);	  
 
	add_option("donators_db_version", "1.0");
}

codex.wordpress: 建表文档

http://codex.wordpress.org.cn/Creating_Tables_with_Plugins

Wordpress数据库说明及wpdb类

以下内容为网络整理资料非原创 

下面是wp-2.0.5版的数据库结构图(E-R图)。这里仅列出了主键和外键。图中菱形表示1:n的关系,白色部分为1,黑色部分为n。

wordpress-db-er-diagram.png

Wordpress共有10个表,按照功能大致分为四类。

  • user: 用户信息,包括wp_users表和wp_usermeta表。
  • post: 文章及评论信息,包括wp_posts、wp_postmeta、wp_comments、wp_post2cat以及wp_categories五个表。
  • link: 链接信息,包括wp_links表和wp_linkcategories表。
  • option: 全局设置信息,包括wp_options表。

表的命名规则也很有意思。基本规则总结如下:

  • 保存对象的基本属性,命名为 wp_objects,使用复数(如 wp_posts,wp_comments);
  • 保存对象的扩展属性,命名为 wp_objectmeta,使用单数(如wp_postmeta,wp_usermeta);
  • 多对多关系,命名为 wp_a2b,其中a和b分别为多对多关系两端的对象名的缩写(如wp_post2cat)。
WPDB类

http://codex.wordpress.org.cn/Function_Reference/wpdb_Class

http://codex.wordpress.org/Function_Reference/wpdb_Class

 数据库说明

http://codex.wordpress.org/Database_Description#Table_Overview

wp_categories: 用于保存分类相关信息的表。包括了5个字段,分别是:

  • cat_ID – 每个分类唯一的ID号,为一个bigint(20)值,且带有附加属性auto_increment。
  • cat_name – 某个分类的名称,为一个varchar(55)值。
  • category_nicename – 指定给分类的一个便于记住的名字,也就是所谓的slug,这是一个varchar(200)值。
  • category_description – 某个分类的详细说明,longtext型值。
  • category_parent – 分类的上级分类,为一个int(4)值,对应是的当前表中的cat_ID,即wp_categories.cat_ID。无上级分类时,这个值为0。

Click to continue reading

WordPress 开发索引

参考文献

WordPress开发文档目录
WordPress开发导言
WordPress API hooks的文章,你可以学到如何去使用它们的知识
WordPress hooks List

推荐阅读:

自己动手写 WordPress 插件:使用 API
制作一个简单的WP插件
如何在wordpress插件中实现ajax

主要API研究

WordPress插件开发需熟知的4个函数 [load]
register_form 用来在注册页面插入密码输入框(当然你也可以插入别的内容),
register_post 用来截获输入的密码,
user_register 用来往数据库中写入刚才截获的密码(大家可能会问, 为什么不直接在register_post 的 action 中直接写入密码呢? 其实我也想这么做的, 可惜不行。
login_message用来在注册页面提示消息, 比如说什么时候你输错了密码, 总要给你反馈个消息吧? 就靠这个东西了。
action(wp_head) [load]

wp_head()的引用

一直使用插件不正常,所有插件都没有引用它们的css,jp。无意中换了个theme就没有问题了,研究一下发现主题中的index.php,page.php,single.php中没有<?php wp_head();?>。

原来wp_head()用于获取插件所需要插到html本头<head></head>的信息。所以有使用插件的WP一定要在theme相关页的<head></head>之间加上它:<?php wp_head();?>。本人建议把它写在head.php中,再由其它模板调用。

Page 1 of 212