PHP学习笔记(六)
《Wordpress 50个过滤钩子》 1-10
过滤钩子是一类函数,wordpress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定点执行。本质上,就是在wordpress输出之前,将对浏览数据做出反应。
添加过滤钩子: add_filter($tag, $function_to_add, $piority, $accepted_tags);
参数解释: $tag(必须):过滤钩子的名称
$funciton_to_add(必须): 要挂载的函数名。
$piority: 整数,判断函数什么时候执行,默认是10,数值越低,优先级越高。
$accepted_tages: 整数,默认是1,设置接收参数的个数。
移除过滤钩子:remove_filter($tag, $function_to_remove, $piority)
应用钩子: apply_filter($tag, $value, $var1, $var2,....)
参数解释: $tag(必须):钩子的名称
$value(必须): 通过add_filter()挂载的过滤函数要修改的值
1. log_errors: 改变默认登录错误信息
默认的错误信息显得比较啰嗦,如果需要简单的错误信息,可以使用该过滤钩子对错误信息进行修改然后返回。
<?php
add_filter( 'login_errors', 'login_errors_example' );
function login_errors_example( $error ) {
$error = 'this is the modified error';
return $error;
}
?>
这样,当登录失败的时候,就会显示 this is the modified error
2. comment_post_redirect: 更改提交评论后的显示页面
当用户提交完评论后,默认是留在同一页面的,当你有需求在评论后跳转到另外一个页面时,可以用这个钩子进行页面指定。
<?php
add_filter( 'comment_post_redirect', 'comment_post_redirect_example' );
function comment_post_redirect_example( $location ) {
return '/thanks-for-your-comment/';
}
?>
$location是默认的页面地址。
3. allowed_redirect_hosts:增加wp_safe_redirect()允许访问的地址。
默认情况下,wp_safe_redirect()函数仅仅允许站内访问,如果想要实现其他的地址访问,可以用这个钩子来添加地址。
<?php
add_filter( 'allowed_redirect_hosts', 'allowed_redirect_hosts_example' );
function allowed_redirect_hosts_example( $content ) {
$content[] = 'forum.mywebsite.com';
$content[] = 'welcome.mywebsite.com';
$content[] = 'help.mywebsite.com';
return $content;
}
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts
?>
$content是数组,存储着允许访问站点的地址。
4.body_class: 给<body>标签加css类。
如果需要给特定的页面指定css的时候,可以通过该钩子给body标签加上css类。
<?php
add_filter( 'body_class', 'body_class_example' );
function body_class_example( $classes ) {
if( is_single() ) {
foreach( get_the_category( get_the_ID() ) as $category )
$classes[] = 'cat-' . $category->category_nicename;
}
return $classes;
}
// Example source: https://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters
?>
5.locale:改变地区(针对翻译功能).
通过该钩子,可以改变地区从而让系统改变读取的翻译文件。
<?php
add_filter( 'locale', 'locale_example' );
function locale_example( $lang ) {
if ( 'tr' == $_GET['language'] ) {
return 'tr_TR';
} else {
return $lang;
}
}
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/locale
?>
6.sanitize_user:过滤username
通过该钩子,可以对用户登录时输入的username进行操作,如转换为小写,字符检查等。
<?php add_filter( 'sanitize_user', 'strtolower' ); // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user ?>
7.the_content:过滤post的内容
对于post的内容,如果需要进行操作,如字符串替换,给文章插入标记等等。可以使用该过滤钩子
<?php
add_filter( 'the_content', 'the_content_example' );
function the_content_example( $content ) {
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
// Example source: http://wpsnipp.com/index.php/functions-php/remove-p-tag-from-around-images-in-the_content/
?>
8.the_password_form:过滤password form
对于带有密码保护的post, wordpress会自定将其替换为password form, 使用该钩子你可以访问和自定义这个form.
<?php
add_filter( 'the_password_form', 'the_password_form_example' );
function the_password_form_example() {
$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">';
$output .= '<span>' . __( "Enter the password:" ) . ' </span>';
$output .= '<input name="post_password" type="password" size="20" />';
$output .= '<input type="submit" name="Submit" value="' . esc_attr__( "Go" ) . '" />';
$output .= '</form>';
return $output;
}
// Example source: http://codex.wordpress.org/Using_Password_Protection#Password_Form_Text
?>
9.the_terms: 过滤the_trems() 函数。
使用该钩子可以过滤函数the_terms()的输出,例如去除其中的标签:
<?php add_filter( 'the_terms', 'strip_tags' ); ?>
10. wp_mail_from: 改变邮箱发件人的地址。
如果你想使用wordpress 发送邮件功能时改变发件人的地址,用该钩子就可以实现。
<?php
add_filter( 'wp_mail_from', 'wp_mail_from_example' );
function wp_mail_from_example( $email ) {
return 'my.email.address@mywebsite.com';
}
?>
英文原文:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-the-first-10-filters--cms-21295
PHP学习笔记(六)的更多相关文章
- java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)
java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- python3.4学习笔记(六) 常用快捷键使用技巧,持续更新
python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...
- Go语言学习笔记六: 循环语句
Go语言学习笔记六: 循环语句 今天学了一个格式化代码的命令:gofmt -w chapter6.go for循环 for循环有3种形式: for init; condition; increment ...
- 【opencv学习笔记六】图像的ROI区域选择与复制
图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...
- Linux学习笔记(六) 进程管理
1.进程基础 当输入一个命令时,shell 会同时启动一个进程,这种任务与进程分离的方式是 Linux 系统上重要的概念 每个执行的任务都称为进程,在每个进程启动时,系统都会给它指定一个唯一的 ID, ...
- # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)
目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Redis学习笔记六:持久化实验(AOF,RDB)
作者:Grey 原文地址:Redis学习笔记六:持久化实验(AOF,RDB) Redis几种持久化方案介绍和对比 AOF方式:https://blog.csdn.net/ctwctw/article/ ...
随机推荐
- 【CSS】Intermediate4:Background Images
1. background:background-color url-background-image background-repeat(repeat/repeat-y/repeat-x/no-re ...
- java中字符串切割的方法总结
StringTokenizer最快 ,基本已经不用了,除非在某些需要效率的场合.Scanner最慢. String和Pattern速度差不多.Pattern稍快些. String和Pattern的sp ...
- 【解决】Oracle数据库实现ID自增长
Oracle数据库要实现ID的自增长,需要创建一个序列和触发器来实现,略微有一点点麻烦,但是也是可以解决的. 直接上地址,首先,http://www.wlcrane.com/article.aspx? ...
- CURL超时处理
一般会设置一个超时时间1S,就是说如果php那边在1S内没有返回给urlserver的话就忽略掉该请求,及不阻塞等待返回了,直接处理下面的操作. 现在php那边有时候会卡,这样一卡就无法再1S内返回消 ...
- Hadoop中java.lang.ClassCastException: partition解决方法
java.lang.ClassCastException: partition.KpiWritable cannot be cast to org.apache.hadoop.io.LongWrita ...
- HW2.19
public class Solution { public static void main(String[] args) { char outcome; long randomNumber; wh ...
- 分布式定时任务框架比较,spring batch, tbschedule jobserver
分布式定时任务框架比较,spring batch, tbschedule jobserver | 移动开发参考书 分布式定时任务框架比较,spring batch, tbschedule jobser ...
- POJ2923--Relocation(01背包+状压dp)
果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Mem ...
- MySQL开发中常用的查询语句总结
1.查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>, ...
- 操作系统课堂笔记(2)操作系统的硬件环境之I/O技术和时钟
I/O技术 1.程序控制I/O技术 有处理器提供相关的IO指令来实现的.主要缺陷是,处理器必须关注IO处理单元的状态,因而它会耗费大量的时间轮询以获得这个信息,这严重降低了系统性能. 2.中断驱动I/ ...