说明:本教程仅限学习,高手请绕道
开发程序:WordPress 3.9-RC1
使用主题:Twenty Fourteen
在开始之前,需要注意三件事情
I、给插件取一个个性化的名字,越个性化越好,以防和其他插件重名冲突。
II、代码里面,一定要包含注释,不为自己,也要为想修改你代码的别人想想。
III、尽量用最新版的 WordPress 进行开发和测试。
插件名称和插件结构
一般来说,都是在 wp-content\plugins 目录下建立一个文件夹,文件夹名就是插件的名称,我们插件名字为"copyright_plugin",文件结构如下。
下面展示出全部代码:
/*
Plugin Name: Copyright plugin
Plugin URI: http://www.xxxx.com/plugins/
Description: 此插件将在文章正文最下面,显示一行版权信息
Version: 1.0.0
Author: xcxc
Author URI: http://www.xxxx.com/
License: GPL
*/
/* 注册激活插件时要调用的函数 */
register_activation_hook( __FILE__, 'display_copyright_install');
/* 注册停用插件时要调用的函数 */
register_deactivation_hook( __FILE__, 'display_copyright_remove' );
function display_copyright_install() {
/* 在数据库的 win_options 表中添加一条记录,第二个参数为默认值 */
add_option("display_copyright_text", "本站点所有文章均为原创,转载请注明出处!
", '', 'yes');
add_option("display_url_text", "www.baidu.com", '', 'yes');
}
function display_copyright_remove() {
/* 删除 win_options 表中的对应记录 */
delete_option('display_copyright_text');//新增一条记录值
delete_option('display_url_text'); //新增一条记录值
}
if( is_admin() ) {
/* 利用 admin_menu 钩子,添加菜单 */
add_action('admin_menu', 'display_copyright_menu');
}
function display_copyright_menu() {
/* add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function); */
/* 页名称,菜单名称,访问级别,菜单别名,点击该菜单时的回调函数(用以显示设置页面) */
//add_options_page()函数可以设置在不同菜单下,教程地址:https://www.cnblogs.com/xcxc/p/3664449.html
add_options_page('Set Copyright', '版本菜单设置', 'administrator','display_copyright', 'display_copyright_html_page');
}
function display_copyright_html_page() {
?>
Set Copyright
method="post" action="options.php">
/* 下面这行代码用来保存表单中内容到数据库 */ ?>
wp_nonce_field('update-options'); ?>
type="text" name="display_url_text" id="display_url_text" value=" get_option('display_url_text'); ?>">
type="hidden" name="action" value="update" />
type="hidden" name="page_options" value="display_copyright_text" />
type="hidden" name="display_url_text" value="display_url_text" />
type="submit" value="保存" class="button-primary" />
}
add_filter( 'the_content', 'display_copyright' );
/* 这个函数在日志正文结尾处添加一段版权信息,并且只在 首页 页面才添加 */
function display_copyright( $content ) {
// if( is_home() )
$content = $content . get_option('display_copyright_text');
return $content;
}
?>
然后首页index.php 页面添加下面代码即可,不添加有也可以,因为the_content 是系统默认自定钩子
if(function_exists('display_copyright')) { echo display_copyright(); }
Powered by Froala Editor