這個 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