Wordpress 通过插件机制,使一套基于基本的 CMS 系统的博客系统有了很大的扩展能力。
第一、WP 的插件与文件系统的接口为 wp-content/plugins 文件夹。接口主文件可以直接放置在该文件夹下,如果牵涉的文件较多,也可以再建立一个文件夹,放在该文件夹下,但该文件夹应该与接口主文件的主文件名相同:如主接口文件为 myplugin.php,则文件夹名字应该为 myplugin。
插件大体上可划分为两个部分:前面是注释,注释下是代码区。
注释部分大体如下示例:
/*
Plugin Name: MyCopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: 网眼
Author URI: http://blog.why100000.com
Description: 把字符串“<!--mycopyrigth-->”替换为版权信息:show copyright once there are letters match “<!--mycopyrigth-->”.
you should config your copyright information in this file,with this verison.
*/
不要简单的看待以上的注释,不是随便写的,是固定的格式。冒号前面的形如“Plugin Name”的信息都是有用的,作为插件的相关信息,会出现在系统设置等菜单下,作为本插件的标识信息。冒号后面的信息可以根据需要改写。冒号连接的一行信息类似于表中的一条记录,且不能换行。
再例如,WP2.3.3 有个插件,其注释如下:
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/#
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.5
Author URI: http://photomatt.net/
*/
第二、代码部分就复杂了。不过一般应该有类似
add_action('admin_footer', 'hello_dolly');
的 PHP 代码,也就是至少需要执行 add_action 函数,当 WP 执行 admin_footer 函数的时候,执行 自定义的 hello_dolly 函数。hello_dolly 函数中就写有特点插件的逻辑。
大家可以去看插件 hello.php,很简单,功能是:当点击 WP 后台管理的菜单项时,在页面右上角随机出现一些类似“毛主席语录”的“口号”(插件的Description里准确描述了其功能)。插件里有两处用了 add_action 函数:
add_action('admin_footer', 'hello_dolly');
add_action('admin_head', 'dolly_css');
分别执行了自定义函数 hello_dolly 和 dolly_css。分别用于显示信息和定义 css 样式。
第三、我模仿网络上的例子,写了一个最简化的 WP 插件,全部代码如下:
<?php
/*
Plugin Name: MyCopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: 网眼
Author URI: http://blog.why100000.com
Description: 把字符串“<!--mycopyrigth-->”替换为版权信息:show copyright once there are letters match “<!--mycopyrigth-->”.
you should config your copyright information in this file,with this verison.
*/
//WP执行the_content函数时,调用插件自定义函数why100000_showcopyright,对文章内容进行过滤
add_filter('the_content', 'why100000_showcopyright');
function why100000_showcopyright($content)
{
$search = "<!--mycopyright-->";
$replace = "<font color='red'>本文由网眼原创。转载请注明<a href='http://blog.why100000.com' target='_blank'>原创作者:网眼(张庆)</a></font>";
$content= str_replace($search, $replace, $content);
return $content;
}
?>
功能是:把文章中的字符串“<!--mycopyrigth-->”替换为版权信息字符串。add_filter 其实是一个回调函数,用于在 WP 的 the_content 函数执行的时候,处理一些事务。具体意义参见自定义 why100000_showcopyright 函数。
这个简单的例子可以增加一些额外的功能,以使其更强大。本文附带了一个已经扩展了部分功能的例子,大家可以参考。
下载:http://blog.why100000.com/Uploads/rar/myCopyright.rar
这仅仅是 WP 插件的应用层的知识。要进一步了解 WP 的插件机制,需要阅读 WP 的源代码。好在是开源的,应该从里面学到更多的经验和技巧。
作者:张庆(网眼) 2009-4-1
来自“网眼视界”:http://blog.why100000.com
“十万个为什么”电脑学习网:http://www.why100000.com
附,hello.php插件源代码,保存到 plugin 文件夹下即可用,在WP2.3.3下正常使用:
<?php /* Plugin Name: Hello Dolly Plugin URI: http://wordpress.org/# Description: This is not just a plugin,it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page. Author: Matt Mullenweg Version: 1.5 Author URI: http://photomatt.net/ */ // These are the lyrics to Hello Dolly $lyrics = "Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when So, take her wrap, fellas Find her an empty lap, fellas Dolly'll never go away again Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when Golly, gee, fellas Find her a vacant knee, fellas Dolly'll never go away Dolly'll never go away Dolly'll never go away again"; // Here we split it into lines $lyrics = explode("/n", $lyrics); // And then randomly choose a line $chosen = wptexturize( $lyrics[ mt_rand(0, count($lyrics) - 1) ] ); // This just echoes the chosen line, we'll position it later function hello_dolly() { global $chosen; echo "<p id='dolly'>$chosen</p>"; } // Now we set that function up to execute when the admin_footer action is called add_action('admin_footer', 'hello_dolly'); // We need some CSS to position the paragraph function dolly_css() { echo " <mce:style type='text/css'><!-- #dolly { position: absolute; top: 2.3em; margin: 0; padding: 0; right: 1em; font-size: 16px; color: #f1f1f1; } --></mce:style><style type='text/css' mce_bogus="1"> #dolly { position: absolute; top: 2.3em; margin: 0; padding: 0; right: 1em; font-size: 16px; color: #f1f1f1; } </style> "; } add_action('admin_head', 'dolly_css'); ?>