《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. A题进行时--浙大PAT 1001-1010

    pat链接:http://pat.zju.edu.cn 1 #include<stdio.h> 2 int main(){ 3 int a,b; 4 int c; 5 while(scan ...

  2. python 探索(四) Python CookBook 系统管理

    看CookBook就像看小说,挑感兴趣的先学习. 所以继<文本>之后,开始<系统管理>. 同样,请善用目录. 发现一个cookbook:好地址 生成随机密码 from rand ...

  3. Jquery zTree的使用

    <%@ page language="java" pageEncoding="utf-8"%><%@taglib prefix="s ...

  4. common

    lexical_cast 提供string2int, int2string, #define(...) 可变宏:-和__VA_ARGS__  宏定义中参数列表的最后一个参数为省略号(三个英文句号,省略 ...

  5. 【开发工具 - Git】之Git版本回退

    这篇博客主要记录了关于 查看记录.版本回退.添加标签.删除文件 的操作 1.查看文件修改情况: 可以通过 git diff a.java查看a.java文件自从上次提交后的修改情况,如果自从上次提交之 ...

  6. Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法

    java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind 此异常的原因是服务器端口被占用 所以解决办法是: 一 ...

  7. Hyper-V介绍

    Hyer-v主机是高端虚拟主机用户的最佳选择.您不再受其他用户程序对您造成的影响,您将得到的是更加公平的资源分配,远远低于虚拟主机的故障率.Hyper-V的分区包含两种:父分区和客户分区.Hyper- ...

  8. HTTP POST发消息

    业务需求:模拟TANX给DSP发消息,protobuf数据已弄好. 代码: def PostDataToDSP(self,url,postdata): headers = { #taobao文档规定 ...

  9. dubbo使用方法

    dubbo使用方法. Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载. ...

  10. java20 创建服务器:ServerSocket

    创建服务器:ServerSocket import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...