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

 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的内容,如果需要进行操作,如字符串替换,给文章插入标记等等。可以使用该过滤钩子

<?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', '123', $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 发送邮件功能时改变发件人的地址,用该钩子就可以实现。

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

原文地址:https://www.cnblogs.com/JacobQiao/p/5233225.html