这个 WP 插件,除过能够在插件管理面板管理外(可以被开启和禁用),还能够在“设置”菜单下对插件进行配置,使插件的功能可以得到扩展。本插件,可以实现对博客文章中的任意字符串进行替换。
以下代码中,why100000_keyword 和 why100000_replace 作为选项文本域标识变量,其值被 option.php 取到并保存到数据库的 wp_options 表中。
get_option("why100000_keyword") 和 get_option("why100000_replace") 语句用于从 wp_options 表中取出数据。
<?php
/*
Plugin Name: MyCopyright
Plugin URI: http://www.why100000.com
Version: 0.1
Author: 网眼(张庆-陕西西安-开开工作室-http://www.myopenit.cn)
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.
*/
//加到“设置”主菜单下
add_action('admin_menu', 'why100000_add_options_page');
function why100000_add_options_page()
{
add_options_page('MyCopyright', 'MyCopyright-Config', 8, 'mycopyright.php', 'why100000_myCopyright_mainpage');
}
function why100000_myCopyright_mainpage()
{
?>
<form name="formamt" method="post" action="options.php">
<?php wp_nonce_field('update-options') ?>
<div class="wrap">
MyCopyright is active.<br><br>
<h2>MyCopyright Config (版权插件配置)</h2>
<div id="msgall">
<fieldset class="options">
<legend>keyword(原字符串):</legend>
<p>
<textarea style="width: 80%;" rows="5" cols="40" name="why100000_keyword"><?php echo get_option("why100000_keyword");?></textarea>
</p>
</fieldset>
<fieldset class="options">
<legend>replacement(代替的字符串):</legend>
<p>
<textarea style="width: 80%;" rows="5" cols="40" name="why100000_replace"><?php echo get_option("why100000_replace");?></textarea>
</p>
</fieldset>
</div>
<p class="submit">
<input type="submit" value="<?php _e('Update Options »') ?>" name="update_message"/>
</p>
<input type="hidden" name="action" value="update">
<input type="hidden" name="page_options" value="why100000_keyword,why100000_replace">
</div>
</form>
<?php
}
add_action('activate_mycopyright/mycopyright.php', 'why100000_copyright_install');
//插件第一次被“启用”时执行,作为初始值保存到表wp_options中
function why100000_copyright_install()
{
add_option("why100000_keyword", "<!--mycopyright-->");
add_option("why100000_replace", "我的版本");
}
//WP执行the_content函数时,调用插件自定义函数why100000_showcopyright,对文章内容进行过滤
add_filter('the_content', 'why100000_showcopyright');
function why100000_showcopyright($content)
{
$search = get_option("why100000_keyword"); //"<!--mycopyright-->";
$replace= get_option("why100000_replace"); //代替的字符串;
$content= str_replace($search, $replace, $content);
return $content;
}
?>
作者:张庆(网眼) 2010-4-4
来自“网眼视界”:http://blog.why100000.com
“十万个为什么”电脑学习网:http://www.why100000.com