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: 改变默认登录错误信息

默认的错误信息显得比较啰嗦,如果需要简单的错误信息,可以使用该过滤钩子对错误信息进行修改然后返回。

<?phpadd_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: 更改提交评论后的显示页面

当用户提交完评论后,默认是留在同一页面的,当你有需求在评论后跳转到另外一个页面时,可以用这个钩子进行页面指定。

<?phpadd_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()函数仅仅允许站内访问,如果想要实现其他的地址访问,可以用这个钩子来添加地址。

 1 <?php
 2  
 3 add_filter( 'allowed_redirect_hosts', 'allowed_redirect_hosts_example' );
 4  
 5 function allowed_redirect_hosts_example( $content ) {
 6     $content[] = 'forum.mywebsite.com';
 7     $content[] = 'welcome.mywebsite.com';
 8     $content[] = 'help.mywebsite.com';
 9     return $content;
10 }
11  
12 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts
13  
14 ?>

$content是数组,存储着允许访问站点的地址。

4.body_class: 给<body>标签加css类。

如果需要给特定的页面指定css的时候,可以通过该钩子给body标签加上css类。

 1 <?php
 2  
 3 add_filter( 'body_class', 'body_class_example' );
 4  
 5 function body_class_example( $classes ) {
 6     if( is_single() ) {
 7         foreach( get_the_category( get_the_ID() ) as $category )
 8             $classes[] = 'cat-' . $category->category_nicename;
 9     }
10     return $classes;
11 }
12  
13 // Example source: https://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters
14  
15 ?>

5.locale:改变地区(针对翻译功能).

通过该钩子,可以改变地区从而让系统改变读取的翻译文件。

 1 <?php
 2  
 3 add_filter( 'locale', 'locale_example' );
 4  
 5 function locale_example( $lang ) {
 6     if ( 'tr' == $_GET['language'] ) {
 7         return 'tr_TR';
 8     } else {
 9         return $lang;
10     }
11 }
12  
13 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/locale
14  
15 ?>

6.sanitize_user:过滤username

通过该钩子,可以对用户登录时输入的username进行操作,如转换为小写,字符检查等。

1 <?php
2  
3 add_filter( 'sanitize_user', 'strtolower' );
4  
5 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user
6  
7 ?>

7.the_content:过滤post的内容

对于post的内容,如果需要进行操作,如字符串替换,给文章插入标记等等。可以使用该过滤钩子

<?phpadd_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.

 1 <?php
 2  
 3 add_filter( 'the_password_form', 'the_password_form_example' );
 4  
 5 function the_password_form_example() {
 6     $output  = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">';
 7     $output .= '<span>' . __( "Enter the password:" ) . ' </span>';
 8     $output .= '<input name="post_password" type="password" size="20" />';
 9     $output .= '<input type="submit" name="Submit" value="' . esc_attr__( "Go" ) . '" />';
10     $output .= '</form>';
11     return $output;
12 }
13  
14 // Example source: http://codex.wordpress.org/Using_Password_Protection#Password_Form_Text
15  
16 ?>

9.the_terms: 过滤the_trems() 函数。

使用该钩子可以过滤函数the_terms()的输出,例如去除其中的标签:

1 <?php
2  
3 add_filter( 'the_terms', 'strip_tags' );
4  
5 ?>

10. wp_mail_from: 改变邮箱发件人的地址。

如果你想使用wordpress 发送邮件功能时改变发件人的地址,用该钩子就可以实现。

<?phpadd_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

 

转载于:https://www.cnblogs.com/JacobQiao/p/5233225.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374961.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JS 操作 radio input(cc问卷管理)

1、选中特定的单选按钮 function showDetail(content){$("input[name^radio]").removeAttr("checked");for(var i0;i<content.length;i){$("#radio"(i1)content.substr(i,1)).attr("checked","checked");} }2、手动添加问…

国内外著名黑客杂志

国外黑客杂志&#xff1a; 《phrack》黑客杂志 http://www.phrack.org 《phrack》创刊于80年代&#xff0c;是世界级的顶级黑客杂志&#xff0c;每年只有一期&#xff0c;现已出了65期&#xff0c;国人似乎至今只有三人在上面发表发表文章&#xff0c;三人好像都是绿盟的人&…

团体项目随笔

我们的团体项目不仅在在课堂上讨论了很久&#xff0c;课后也是几经讨论。每个人都有不同的想法我特别想做一个基于Web编写的驴客网&#xff0c;因为基于个人需求&#xff0c;在最终的讨论中被毙掉。 我们组最终的的讨论结果是写个游戏&#xff0c;关于游戏的发展&#xff0c;这…

Apache Lucene拼写检查器的“您是不是要”功能

Google的“您是不是要”功能 在上一篇文章中对Lucene进行了介绍之后 &#xff0c;现在是时候提高它&#xff0c;创建一个更复杂的应用程序了。 您肯定最熟悉Google的“您是不是要”功能&#xff08;其他搜索引擎也支持此功能&#xff09;。 这是一个例子&#xff1a; Lucene …

Android-做个性化的进度条

1.案例效果图 2.准备素材 progress1.png(78*78) progress2.png(78*78) 3.原理 采用一张图片作为ProgressBar的背景图片(一般采用颜色比较浅的)。另一张是进度条的图片(一般采用颜色比较深的图片)。进度在滚动时&#xff1a;进度图片逐步显示&#xff0c;背景图片逐…

汇编小记16/3/27

最后更新2016-03-27 21:05:06 [address]与[bx] [address] 在debug中mov ax,[0] 等价于mov ax,ds:[0] [0]表示内存偏移地址 但是在masm汇编解释器中&#xff0c;mov ax,[0] 等价于mov ax,0 [0]表示常量0 [bx] mov ax,[bx] 表示 bx存放的数据为一个偏移地址&#xff0c;段…

ConcurrentLinkedHashMap v 1.0.1发布

大家好&#xff0c;我们发布了并发LinkedHashMap实现的1.0.1版本。 在最新版本中&#xff0c;已进行了一些较小的修改&#xff0c;以在多个线程遍历映射的元素时提高性能。 最新版本还引入了可插拔驱逐策略。 当然&#xff0c;您可以实现自定义逐出策略&#xff0c;也可以将它…

BOMbing The System

roy g bivFebruary 2011 [Back to index] [Comments (0)] What is a BOM? Why should we care? Great, can we do that? Okay, lets do it! Unicode in files Greets to friendly people (A-Z) What is a BOM? Its not the thing that explodes. Thats a BOMB. Heh. BO…

鸟哥的linux私房菜学习笔记 ---第7章-2

1,文件内容查阅的命令: cat ,tac nl,more, less,head,tail ,od 文件的查阅参数,显示行号如何显示行号 nl 中的所有参数都是关于如何显示行号的 这里面less的功能更多,更灵活 :空格 下一页 pageup上一页 pagedown 下一页 /string 字符串查询 ?string 反向字符串查询 man的命…

HDU - 4497 GCD and LCM

题意&#xff1a;给出三个数的gcd,lcm&#xff0c;求这三个数的全部的可能 思路 &#xff1a;设x,y,z的gcd为d&#xff0c;那么设xd*a&#xff0c;yd*b&#xff0c;zd*c。a&#xff0c;b。c肯定是互质的。那么lcmd*a*b*c,所以我们能够得到a*b*clcm/gcdans,将ans分解因数后&…

Java Lambda语法替代

关于lambda-dev邮件列表的讨论已经开始解决lambdas /函数文字的Java语言语法应该是什么样的问题。 让我们看一个稍微平凡的例子&#xff0c;然后尝试弄清楚问题。 Perl的人有一个很好的例子&#xff0c;说明以某种功能性的方式使用函数引用–他们称其为Schwartzian变换&#xf…

浅析SMC技术

今天让我们来看Win32ASM里面的高级一点的技术——SMC&#xff08;当当当当……&#xff09;&#xff01;&#xff01;&#xff01;SMC是什么意思&#xff1f;它的英文名叫“Self Modifying Code”&#xff0c;顾名思义&#xff0c;就是“代码自修改”&#xff08;&#xff1f;&…

JAVA基础--程序是顺序执行的

class Testa {public static void main(String[] args) {String aa"aaa";String bb"bbb"aa;aa"cccc";System.out.println(bb);} } 输出的是 “bbbaaa class Testa {public static void main(String[] args) {String aa"aaa";String …

Spring MVC拦截器示例

我以为是时候看看Spring的MVC拦截器机制了&#xff0c;这种机制已经存在了很多年&#xff0c;并且是一个非常有用的工具。 Spring Interceptor会按照提示进行操作&#xff1a;在传入的HTTP请求到达您的Spring MVC控制器类之前对其进行拦截&#xff0c;或者相反&#xff0c;在其…

Android 调用系统的分享[完美实现同一时候分享图片和文字]

android 系统的分享功能 private void share(String content, Uri uri){Intent shareIntent new Intent(Intent.ACTION_SEND); if(uri!null){//uri 是图片的地址shareIntent.putExtra(Intent.EXTRA_STREAM, uri);shareIntent.setType("image/*"); //当用户选择短信时…

团队行为守则—如果你们由我来领导

&#xfeff;&#xfeff;如果你是在我领导的团队里&#xff0c;有几个额外的事情我要告诉你。我深信这些行为守则是一个高效团队的润滑剂&#xff0c;我并不只是要求别人这样做&#xff0c;我自己也严格恪守。 只有三样事&#xff1a; 问&#xff1a;如果你对任务不清楚&#…

做短,但做对!

编写简洁&#xff0c;优雅&#xff0c;清晰的代码一直是开发人员的艰巨任务。 您的同事不仅会感谢您&#xff0c;而且您会惊讶地发现&#xff0c;不断期待着重构解决方案以更少的代码完成更多&#xff08;或至少相同&#xff09;的工作是多么令人兴奋。 曾经有人说好的程序员是…

math

莫比乌斯反演&#xff1a; $F(n) \sum\limits_{d|n} {f(d)} \Leftrightarrow \sum\limits_{d|n} {\mu (d)F(\frac{n}{d})} $ 其中 ${\mu (d)}$为莫比乌斯函数: 若$d$等于0 , 则${\mu (d)}$1 若$d {p_1}{p_2}{p_3}...{p_k}$ , ${p_i}$为互异质数&#xff0c;则${\mu (d)}$${( …

(笔试题)二进制1的个数相同的距离最小数

题目&#xff1a; 输入&#xff1a;整数A输出&#xff1a;整数B条件&#xff1a;A和B的二进制1的个数相同&#xff0c;且A和B之间的距离|A-B|最小。思路&#xff1a; 题目没有说明整数类型&#xff0c;这里认为是带符号的整数&#xff0c;即区分正负数。 根据题意&#xff0c;A…

Java Swing –日期选择器对话框

房子里有Swing开发人员吗&#xff1f; 对于使用Swing的用户来说&#xff0c;这是一个GUI组件&#xff0c;可能会对您的UI编码工作有所帮助。 我们的JCG合作伙伴之一提供了日期选择器小部件。 一探究竟&#xff1a; Java Swing –日期选择器对话框以选择日期 翻译自: https://…