WordPress插件制作笔记(一)---First Plugins Demo
1->add_action HOOK简单说明: http://codex.wordpress.org/Plugin_API/Action_Reference (参考网址)
//在后台页脚位置加载(执行)函数 add_str 为自定义的函数名
add_action('admin_footer','add_str'); //在后台头部位置加载(执行)函数
add_action('admin_head','add_str'); //在wp加载之前加载(执行)函数
add_action('wp_loaded','add_str'); //当每加载一篇文章的时候加载(执行)函数
add_action('the_post','add_str'); //当更新文章的时候加载(执行)函数
add_action('save_post','add_str'); function add_str() {
echo '<h1>Welcome to the plugin (xaiolong production)</h1>';
}
2->add_filter() HOOK简单说明 http://codex.wordpress.org/Plugin_API/Filter_Reference (参考网址)
//给每个文章标题,后面加上id编号,和一个自定义的字符串
add_filter('the_title','write_title',10,2);
function write_title($title,$id){
return $title.'---'.$id.'---create by xiaolong';
} //给每个文章内容后面加上一个自定义的字符串
add_filter('the_content','write_content',10,1);
function write_content($content) {
return $content.'---create by xiaolong';
}
3->插件演示代码:下载地址:http://pan.baidu.com/s/1mg3JpVy
在 wordpress/wp-content/plugins/ 目录下 新建一个文件夹取名为first_plugins_demo(或自己定义),在新建的first_plugins_demo目录下新建first_plugins_demo.php(自定义.php但要求与父级目录名一直)文件,将以下代码拷贝到first_plugins_demo.php文件中保存,刷新wordpress后台-插件菜单-已安装的插件,启用First plugins Demo 插件即可
目录示例:*/wordpress/wp-content/plugins/first_plugins_demo/first_plugins_demo.php
<?php
/*
Plugin Name: First Plugins Demo
Plugin URI: http://www.cnblogs.com/fxmbz/p/4030286.html
Description: First Plugins Demo, admin head add a line information, add the Settings menu button In the Settings menu
Version: 1.0
Author: xiaolong
Author URI: http://www.cnblogs.com/fxmbz/
License: GPL
Text Domain: 29583035
*/ /**
* [register_activation_hook run set_color_options]
*/
register_activation_hook( __FILE__, 'set_color_options' ); /**
* [register_deactivation_hook run del_color_options]
*/
register_deactivation_hook( __FILE__, 'del_color_options' ); /**
* [set_color_options options表插入数据]
*/
function set_color_options() {
// options表插入数据 参数:('$option_name','$option_value')
add_option( 'color','red' );
} /**
* [del_color_options 删除options表数据]
*/
function del_color_options() {
// 删除options表数据 参数:('$option_name')
delete_option( 'color' );
} /**
* [add_action 在wp后台头部位置加载(执行)函数 add_acton('$hook', '$function')]
* [add_str 输出字符串, get_option('color') 获得options表中option_name为color的option_value的值]
*/
add_action( 'admin_head', 'add_str' );
function add_str() {
echo '<h2 style="color:' . get_option( 'color' ) . '">Welcome to first plugins demo! author: Zhangxl, Email: 29583035@qq.com</h2>';
} /**
* [add_action 在后台菜单位置添加一个页面 add_acton('$hook', '$function')]
* [create_admin_page 配置增加页面的title, 菜单栏的title, 权限, slug, $function]
*/
add_action( 'admin_menu', 'create_admin_page' );
function create_admin_page() {
// add_options_page() http://codex.wordpress.org/Function_Reference/add_options_page(参考网址)
// add_options_page( $page_title, $menu_title, $capability(权限), $menu_slug(URL-friendly name), $function );
add_options_page( 'First Plugins Demo Setting','First Plugins Settings','manage_options','firstplugindemo','wp_options_page' );
} /**
* [wp_options_page 输出页面模板]
* @return [type] [description]
*/
function wp_options_page() {
?>
<div class="wrap">
<h2>First Plugins Settings</h2>
<!-- 更新options表的数据 -->
<?php update_color_option(); ?>
<form action="" method="post">
Color: <input type="text" name="color" value="<?php echo get_option( 'color' );?>" />
<input type="submit" name="submit" value="submit" />
</form>
</div>
<?php
} /**
* [update_color_option 更新options表的数据]
* @return [type] [string]
*/
function update_color_option() {
$color = $_POST[ 'color' ];
$rule = "/[a-z]|#([0-9a-zA-Z])/";
// 正则匹配客户输入的颜色代码,为纯字母,或者是#开头的16进制代码
$result = preg_match($rule, $color); if ( $_POST[ 'submit' ] ) {
if ( $result ) {
update_option( 'color', $color );
echo "<p style='color: green; font-size: 18px;'>Update Success Full !</p>";
} else {
echo "<p style='color: red; font-size: 18px;'>Some Thing Wrong ! Please check the Color Name spelling, or a Color input format for 'red' or #aabbcc (Hexadecimal color ode).</p>";
}
} }
http://codex.wordpress.org/Template_Tags (参考网址)
https://codex.wordpress.org/Function_Reference (参考网址)
http://codex.wordpress.org/Plugin_API/Filter_Reference (参考网址)
http://codex.wordpress.org/Plugin_API/Action_Reference (参考网址)
WordPress插件制作笔记(一)---First Plugins Demo的更多相关文章
- WordPress插件制作笔记(二)---Second Plugins Demo
1->插件演示代码:下载地址:http://pan.baidu.com/s/1gd1lFlL 在 wordpress/wp-content/plugins/ 目录下 新建一个文件夹取名为seco ...
- WordPress插件制作笔记(三)---Stars Comments Article
wp 文章星级评价 插件 下载地址4:http://pan.baidu.com/s/1eQnGIGU [articles_star_vote_score_optiontable_serialize_c ...
- WordPress插件制作教程(一): 如何创建一个插件
上一篇还是按照之前的教程流程,写了一篇WordPress插件制作教程概述,从这一篇开始就为大家具体讲解WordPress插件制作的内容.这一篇主要说一下插件的创建方法. 相信大家都知道插件的安装文件在 ...
- WordPress插件制作教程(八): 最后总结
WordPress插件教程最后一篇,还是为大家简单的做下总结.这次插件制作教程讲的内容和知识点个人觉得不是很多,因为插件制作不单单是这些内容,它涉及的知识很多很多,不是说你会一些函数就可以做出一个好的 ...
- WordPress插件制作教程概述
接下来的一段时间里,开始为大家讲解WordPress插件制作系列教程,这篇主要是对WordPress插件的一些介绍和说明,还有一些我们需要注意的地方,以及需要掌握的知识. WordPress插件允许你 ...
- WordPress插件制作教程(二): 编写一个简单的插件
上一篇说到了如何创建一个插件,我想大家看了之后一定会有所收获,这一篇简单给大家写一个插件样例,让大家有一个基本的印象.这个插件的样例就是当你激活这个插件后会在你的每篇文章中插入一段自己定义好的内容,比 ...
- WordPress插件制作教程(六): 插件函数之动作(Actions)函数
这一篇为大家说一下WordPress插件函数吧,要制作插件,了解这些函数是非常有必要的 WordPress插件函数分为“动作”(Actions)和过滤器”(Filters),WordPress 使用这 ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- WordPress插件制作教程(三): 添加菜单的方法
上一篇编写了一个简单的插件,让大家对插件的简单制作有个了解,这一篇我们在更深一步,当我们激活插件后后台会显示菜单出来,然后通过单击菜单显示自己定义好的信息.激活之后会在WordPress后台显示一个菜 ...
随机推荐
- 学习Cassandra资料的一些整理
Cassandra note: 依赖:需要java 8 (http://www.oracle.com/technetwork/java/javase/downloads/index.html) 数据模 ...
- Signing key has not been configured
Signing key has not been configured.https://dev.openwrt.org/changeset/38284 Add package signing key ...
- linux获取CPU温度
Centos系列 1 yum install lm_sensors 2 sensors-detect 3 sensors Ubuntu系列(多了service module-init-tools st ...
- 重写扫雷(基于jQuery) 新手 有不足的地方敬请谅解
记得刚开始学习js的时候写过一次扫雷,一个下午的时间被计算搞死,整个头是晕乎. 入职后,蹭着空闲的时间随手写了一个扫雷. 直接上代码了 (function() { function module() ...
- JavaScript 总结
1. JavaScript prototype属性是一个对象 当一个函数在定义之后 就会自动获得这个属性.其初始值是一个空对象.新建了一个名为Cat的构造函数,其prototype为一个对象,cons ...
- ionic 字体的导入方法
更换字体 这是第一个图标(蜜蜂推荐)是撸主原来的图标 这是UI给的效果图的图标,显然默认的图标不符合要求 查找图标所在的样式,在tabs.html文件中 <ion-tabs class=&quo ...
- excel导入mssql数据库,支持excel2003--2010文件格式
rt,简单的excel导入mssql.未做性能及海量数据优化,一般单表5000左右条数据导入适用. 源码非原创,来源于之前搜集整理,原作者无从考究,如有版权问题请留言注明. 看代码.前台页面是一个Fi ...
- xampp进程和非进程执行
xampp以服务和非服务运行apache有哪些区别?为什么去掉勾是以进程的形式执行?
- activiti总结
1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...
- ADB错误“more than one device and emulator”(转)
当我连着手机充电的时候,启动模拟器调试,执行ADB指令时,报错.C:\Users\gaojs>adb shellerror: more than one device and emulatorC ...