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后台显示一个菜 ...
随机推荐
- Spring Assert.notNull
Exception in thread "main" java.lang.IllegalArgumentException: Source must not be null at ...
- Javascript的IE和Firefox兼容性汇编
以下以 IE 代替 Internet Explorer,以 MF 代替 Mozzila Firefox 1. document.form.item 问题 (1)现有问题: 现有代码 ...
- Signing key has not been configured
Signing key has not been configured.https://dev.openwrt.org/changeset/38284 Add package signing key ...
- 在Excel中将数字设置成文本格式的技巧
在Excel中将数字设置成文本格式的技巧 一个简单的方法,利用[数据]菜单的[分列]功能来将数字设置为文本格式.具体操作步骤为: 1.选中所有需要处理的数字单元格. 2.选择[数据]菜单[分列]功能. ...
- 用Visual Studio创建gtest动态链接库工程
Step1 创建名为gtest的Win32 Project Step2 在Application Settings中的Application type下选择DLL Step3 把gtest-all.c ...
- doxygen学习笔记
下载doxygen:http://www.cnblogs.com/duxiuxing/p/4301015.html 学习思路 doxygen能够根据代码和注释生成文档.可想而知,doxygen对注释的 ...
- N - Optimal Milking - POJ 2112(二分图多重匹配+Floyd+二分搜索)
题意:有K太挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 分析:应该先使用floyd求出来点之间的最短路??(不晓得给 ...
- SQL - 添加外键
不解释: ---先创建外键的column ALTER TABLE tblLicenses ADD ProductID int not null; ---添加外键 ALTER TABLE tblLice ...
- Unity3D基础学习 加载场景时隐藏物体,点击显示时显示物体
隐藏物体有两种方法,一是设置Meshrender为False,即不渲染物体. 二是设置物体为False,禁用物体,我使用的第二种. 当场景中需要隐藏的物体很多时,我们可以添加一个层来表示需要隐藏的物体 ...
- java cmd常用命令
熟悉Java的常用命令 面试例题11:使用jar命令. 请使用jar命令,将test文件夹压缩成.jar文件,并简述其压缩包的结构. 考点:对于Java程序员来说,更多情况下是使用集成Java开发工具 ...