《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学习笔记(六)的更多相关文章

  1. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

  2. Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  3. Typescript 学习笔记六:接口

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  4. python3.4学习笔记(六) 常用快捷键使用技巧,持续更新

    python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...

  5. Go语言学习笔记六: 循环语句

    Go语言学习笔记六: 循环语句 今天学了一个格式化代码的命令:gofmt -w chapter6.go for循环 for循环有3种形式: for init; condition; increment ...

  6. 【opencv学习笔记六】图像的ROI区域选择与复制

    图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...

  7. Linux学习笔记(六) 进程管理

    1.进程基础 当输入一个命令时,shell 会同时启动一个进程,这种任务与进程分离的方式是 Linux 系统上重要的概念 每个执行的任务都称为进程,在每个进程启动时,系统都会给它指定一个唯一的 ID, ...

  8. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

  9. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  10. Redis学习笔记六:持久化实验(AOF,RDB)

    作者:Grey 原文地址:Redis学习笔记六:持久化实验(AOF,RDB) Redis几种持久化方案介绍和对比 AOF方式:https://blog.csdn.net/ctwctw/article/ ...

随机推荐

  1. mini2440触摸屏驱动分析

    mini2440驱动分析系列之 ---------------------------------------Mini2440触摸屏程序分析 By JeefJiang July,8th,2009 这是 ...

  2. extjs Cannot read property 'dom' of null

    如果你的EXTJS报错: Cannot read property 'dom' of null,那就有可能是因为你的HTML或者JSP文件中的BODY标签里面少了个东西比如代码是: <html& ...

  3. WordPress更新服务加快收录

    WordPress更新服务的设置在后台"设置"-"撰写".更新服务下的文本框中默认为http://rpc.pingomatic.com/.该地址网站Ping-o ...

  4. PC-经典之“运行里面的密密”

    msconfig.exe 你自己往里面输入这个字母就可以看到了,试试看,还有,我这里有一些可以在"运行"栏里输入的命令,一并给你: 以下为Windows操作系统的常用运行命令,执行 ...

  5. [二]JFreeChart实践一

    生成一张简单的图片 java代码: package com.lxl.chart; import javax.servlet.http.HttpSession; import org.jfree.cha ...

  6. JavaScript- 获得TreeView CheckBox里选中项的值

    获得TreeView CheckBox里选中项的值,对JSDOM控制还不是很熟,感觉不太容易.试了很多次终于成功了. 代码如下 <body> <form id="form1 ...

  7. 模拟等待事件row lock waits

    是索引块分裂引起的锁等待,往往与enq: TX - index contention 伴随产生,enq:TX - index contention的解释. Waits for TX in mode 4 ...

  8. Windows平台Atom编辑器的常用快捷键小抄Cheat Sheet

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Windows平台Atom编辑器的常用快捷键小抄Cheat Sheet.

  9. mysql字符串分割函数(行转列)

    由于工作需要需要处理一些以逗号分隔的字符串,每次都要现做很是麻烦,网上找了很多都没有现成的,好吧,自己动手写一个好了 )) ) BEGIN /*函数功能: 把带逗号的字符串分割取出 参数: num 要 ...

  10. linux命令行模式下实现代理上网

    有些公司的局域网环境,例如我们公司的只允许使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是linux纯命令行的界面就....下面简单几步就可以实现了! 一.命令行界面的一般代理设置方 ...