50个WordPress过滤钩子(1-10)

善良是人生的正能量,是一种能够面对人生一切困苦的力量,是能够化解一切矛盾和摩擦的力量,是能够带来和平与幸福的力量,是能够让世界越来越美好的力量。
本文是《50个 WordPress 过滤钩子》专题的第 4 篇,共 7 篇:
  • 50个WordPress过滤钩子(21-30)
  • 50个WordPress过滤钩子(11-20)
  • 50个WordPress过滤钩子(31-40)
  • 50个WordPress过滤钩子(1-10)
  • 50个WordPress过滤钩子(介绍过滤钩子)
  • 50个WordPress过滤钩子(41-50)
  • 50个WordPress过滤钩子(总结)

在本系列的第一篇文章中,我们在WordPress的上下文中介绍了过滤钩子的定义。在这个好代码教程,我们将开始学习所选的50个过滤钩子,并举例说明每个钩子是做什么的。

话不多说,下面我们就开始介绍第一批50个过滤钩子。

修改默认登录提示错误信息

当登录出现错误提示时,Wordpress泄露了太多信息,例如:“用户barisunver 输入密码错误”,这意味着黑客可以尝试使用不同的用户名去匹配密码,有了过滤钩子,你就可以禁用此类错误提示信息。

样例:解决泄露信息过多问题

以下代码将所有的错误登录提示信息重置为空字符串,防止黑客轻易尝试出我们的账号密码信息。

1
2
3
4
5
6
7
8
9
10
<?php

add_filter( 'login_errors', 'login_errors_example' );

function login_errors_example( $error ) {
    $error = '';
    return $error;
}

?>

<?php add_filter( 'login_errors', 'login_errors_example' ); function login_errors_example( $error ) { $error = ''; return $error; } ?>

评论完成后重定向到其他页面

在wordpress中,当你发表完评论后,会仍然停留在同个页面,这个是符合逻辑的。但通过过滤钩子,你可以在完成评论后自动重定向到其他页面。comment_post_redirect钩子就可以让我们实现这点。下面是本过滤钩子完美样例。

样例:评论成功后将用户重定向到订阅页面

评论通常都跟随在博客文章的下方,如果在评论完成后将其重定向到另一页面,其内容为:“感谢您的评论,您是否想订阅我的博客呢?”我们就有可能将评论者转化为订阅者,有了可爱的comment_post_redirect钩子过滤,这个任务就很容易可完成。

1
2
3
4
5
6
7
8
9
<?php

add_filter( 'comment_post_redirect', 'comment_post_redirect_example' );

function comment_post_redirect_example( $location ) {
    return '/thanks-for-your-comment/';
}

?>

<?php add_filter( 'comment_post_redirect', 'comment_post_redirect_example' ); function comment_post_redirect_example( $location ) { return '/thanks-for-your-comment/'; } ?>

请注意在wordpress在此处使用了wp_safe_redirect(),意味着必须使用本地页面或者允许的主机页面(见下文中的过滤钩子allowed_redirect_hosts)

使用wp_safe_redirect()函数允许外域重定向

wp_safe_redirect()函数默认只允许wordpress安装后的域名内重定向,不允许外域链接重定向,但通过wp_safe_redirect()这个函数,可以轻易添加外域(相当于添加域名白名单)

样例:允许外域重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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

?>

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

给<body>标签增加自定义CSS类

body_class()函数功能强大,它在不同的页面情况下提供了不同的相应语义化的CSS 标签,使CSS 样式更为有效。使用同名的body_class过滤钩子,可以增加或者删除CSS 类。

样例:给文章页面的<body>标签增加以分类名命名的类名

如果你想为每个分类目录设定不同的风格,可以用过滤钩子body_class来实现,办法是在单个文章页面的body标签中增加分类名称样式,见以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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

?>

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

若有个分类目录叫“world”, 那么在该分类的页面下会在<body> 标签中发现 .cat-world这个类。

更改区域设置设置网站语言

WordPress为全球最佳内容管理系统,因为其支持多语言环境,locale过滤钩子可以让我们在某些情况下设置不同语言。

样例:使用URL参数设置网站的语言

如果你有一个多语言网站,切换网站语言最简单的解决办法就是通过URL参数来实现,见以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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

?>

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

当访问类似mywebsite.com/?language=tr参数网址时候,你的网站语言将切换为土耳其语(Turkish)。当然,并非指的文章的内容,但应该也算是个良好的开端。

过滤用户名的不安全字符

Wordpress可以用sanitize_user()的函数处理用户名,可以使用同名的过滤钩子定制函数。

样例:只接受小写用户名

如果你不想让用户输入大写用户名(不管是“SHOUTINGBOY88” 还是“CrazyGirl92”),均可使用PHP的strtolower函数来hook 这个sanitize_user函数。

1
2
3
4
5
6
7
<?php

add_filter( 'sanitize_user', 'strtolower' );

// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user

?>

<?php add_filter( 'sanitize_user', 'strtolower' ); // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user ?>

这个应该是本系列中最为简单的过滤钩子了。

过滤文章文本内容

这个不需作过多介绍了,the_content过滤器即可允许我们更改文章内容。

样例:移除包裹在<img>标签上的<p>标签

WordPress默认并不允许我们单独在一个段落中展示图片——它会自动添加一个<p>到图片中,这个问题一直困扰着我,如果你也有同样的困惑,可以使用以下的代码段去除这些标签。

1
2
3
4
5
6
7
8
9
10
11
<?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/

?>

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

自定义WordPress 密码文章提示文字

对于那些设置了密码保护的文章,WordPress将以默认的密码提示文字替换文章内容。使用the_password_form过滤钩子,你就可以自定义上述提示信息。

样例:让密码提示信息更为简洁

若你的站点有很多的密码保护文章,而你又不想让其反复出现“This post is password protected(本文有密码保护)”烦人字样,可以使用下列代码段让其更为简洁和人性化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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

?>

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

就这样,只需一行三两个字:一个密码输入框,一个提交按钮即可解决问题。

过滤the_terms()函数

如果你对the_terms()函数的输出不满意,或者只是做些简单改变,可以使用同名的过滤钩子the_terms

样例:用the_terms()函数去除html标签

几年前,我花了很多时间设法让其去除HTML标签(纯文本),最后却发现有如此简单的解决办法。见代码:

1
2
3
4
5
<?php

add_filter( 'the_terms', 'strip_tags' );

?>

<?php add_filter( 'the_terms', 'strip_tags' ); ?>

当我发现可以仅用the_terms过滤钩子Hook到PHP函数即可解决时万分诧异。

修改发送的默认的Email 地址

当WordPress 程序发送邮件的时候,它会使用类似wordpress@yourwebsite.com的邮箱地址作为发件人。借助下面这个过滤钩子,你可以修改它并且自定义发件人的Email 地址。

样例:将你自己的邮箱设置为“发件人”邮箱地址

你可以使用下面代码样例,将你自己的邮箱设置为“发件人”邮箱地址:

1
2
3
4
5
6
7
8
9
<?php

add_filter( 'wp_mail_from', 'wp_mail_from_example' );

function wp_mail_from_example( $email ) {
    return 'my.email.address@mywebsite.com';
}

?>

<?php add_filter( 'wp_mail_from', 'wp_mail_from_example' ); function wp_mail_from_example( $email ) { return 'my.email.address@mywebsite.com'; } ?>

此处可以和wp_mail_from_name配合使用:同样的逻辑,你只要自定义返回的参数的值,你就可以将其自定义为需要的用户名了。

总结

在本文中我们已经学习了50个过滤钩子中的10个,希望你喜欢并能从中学习到新知识。

希望看到大家的想法:你是如何看待这些过滤钩子的?请在评论中写下建议和看法吧。如果你喜欢这些文章,也别忘了将他们分享给你的好友,下个好代码教程再见。

原文出自:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-the-first-10-filters--cms-21295

由 WordPress大学 原创翻译,未经允许,禁止转载和采用本译文。

以上就是50个WordPress过滤钩子(1-10)。不知觉的在玻璃窗上呵出你的名字,原来你一向在我心里。更多关于50个WordPress过滤钩子(1-10)请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?