外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>50个WordPress过滤钩子(11-20)

50个WordPress过滤钩子(11-20)

文本是《50个 WordPress 过滤钩子(共7篇)》专题的第 3 篇。阅读本文前,建议先阅读前面的文章:

在本系列的前一章节,我们开始学习50个WordPress过滤钩子,在上百个过滤器中,我们每章节分别学习10个。

在本教程中,我们将各过滤钩子用法分别举例说明。

下面我们就开始吧。

在WordPress 中处理可翻译的数据

WordPress 中最酷的功能就是几乎每一文本内容都可以译成各种语言。当然,如果你的网站语言是英语,可能就没有这个需求;但不能保证所有人都使用英文网站。

gettext这个过滤器可以让你以不同方式达到此目的,让我们看一个样例:

样例:更正开发者的语法错误

例如你找到了一个非常棒的插件,但你发现插件的开发者的英语水平一般,在代码中有很多的错误的文本代码。幸运的是所有这些字符串都是可以重新翻译的,此时便可用gettext这个过滤钩子:

<?php
 
add_filter( 'gettext', 'gettext_example', 20, 3 );
 
function gettext_example( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'E-meil Adress' :
            $translated_text = __( 'Email Address', 'plugin_text_domain' );
            break;
    }
    return $translated_text
}
 
// Example source: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/
 
?>

优化标题url别名

WordPress 会使用一个名为sanitize_title()的函数优化标题:自动替换空格为“-”并保存为url别名。通过sanitize_title过滤钩子便可以扩展这个函数。

样例:移除标题url别名中的单词“the”

如果你不想让让单词“the”出现在url别名中,就可以使用下面的代码将其删除。

<?php
 
add_filter( 'sanitize_title', 'sanitize_title_example' );
 
function sanitize_title_example( $title ) {
    $title = str_replace( '-the-', '-', $title );
    $title = preg_replace( '/^the-/', '', $title );
    return $title;
}
 
?>

简单而完美的解决办法。

从Texturization 中排除简码

这个便捷的过滤器可以让你指定通过wptexturize()函数不运行哪些简码(shortcode),详情见官方文档

样例:从Texturization 中排除简码

如果你想让某个简码从Texturization 中排除,可将简码名字加入”do not texturize”名单,见下列代码:

<?php
 
add_filter( 'no_texturize_shortcodes', 'no_texturize_shortcodes_example' );
 
function no_texturize_shortcodes_example( $shortcodes ) {
    $shortcodes[] = 'myshortcode';
    return $shortcodes;
}
 
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/no_texturize_shortcodes
 
?>

过滤评论的审批状态

WordPress有一个算法去判定一条评论是否应该定性为垃圾评论,然后才去加入评论待审或已批准列表中。通过pre_comment_approve这个过滤钩子可以让上述定性标准做些许变化。

样例:将长用户名的评论标记为垃圾评论

在土耳其,垃圾评论通常使用很长的用户名,有时URL也很长。

使用下面的代码,可以自动清理类似使用“Domestic and International Shipping With Extremely Low Prices (Click Here for More Information)”这些长名字的垃圾评论。

<?php
 
add_filter( 'pre_comment_approved', 'pre_comment_approved_example', 99, 2 );
 
function pre_comment_approved_example( $approved, $commentdata ) {
    return ( strlen( $commentdata['comment_author'] ) > 75 ) ? 'spam' : $approved;
}
 
// Example source: https://gist.github.com/norcross/5468979
 
?>

对 Andrew Norcross的想法表示感谢!

特别提示:如果你想通过检测评论url是否超过一定长度来判断是否为垃圾评论,那么只需要修改comment_author’为 ‘comment_author_url’即可。

配置“通过邮箱发文章”功能

你是否知道可以通过邮件往WordPress 上发表文章?WordPress 提供了这个平时很少使用的功能,过滤钩子为enable_post_by_email_configuration。

样例:开启或者关闭“通过邮箱发文章”功能

有些情况下(例如安全角度考虑)你或许想关闭该功能,只要下面的一行代码就可以解决。

<?php
 
add_filter( 'enable_post_by_email_configuration', '__return_false', 100 );
 
?>

或者多站点wordpress时想启用(wordpress多站点时候默认是关闭此功能的),你可以使用__return_true() 函数:

<?php
 
add_filter( 'enable_post_by_email_configuration', '__return_true', 100 );
 
?>

过滤页面标题

WordPress 中通过wp_title()函数输出页面标题,也就是你在浏览器标签栏上看到的那个。wp_title函数可以让我们自定义该标题。

样例:重写页面标题(正确的方式)

Tom McFarlin在他的博客中解释了如何自定义页面标题,详情请查阅原文,此处举例如下:

<?php
 
add_filter( 'wp_title', 'wp_title_example', 10, 2 );
 
function wp_title_example( $title, $sep ) {
    global $paged, $page;
 
    if ( is_feed() )
        return $title;
 
    // Add the site name.
    $title .= get_bloginfo( 'name' );
 
    // Add the site description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title = "$title $sep $site_description";
 
    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 )
        $title = sprintf( __( 'Page %s', 'tuts_filter_example' ), max( $paged, $page ) ) . " $sep $title";
 
    return $title;
}
 
// Example source: http://tommcfarlin.com/filter-wp-title/
 
?>

在评论被保存到数据库前进行处理

如果你需要将被保存到数据库前的评论数据(评论id,评论员名称,邮箱地址,网址等等)进行处理,那么preprocess_comment过滤钩子可以帮助你。

样例:将评论内容由大写文字转换为小写

你是否遇到过很多评论内容中每个单词都是大写字母?若是,则可以使用下列代码将其自动转换为小写。

<?php
 
add_filter( 'preprocess_comment', 'preprocess_comment_example' );
 
function preprocess_comment_example( $commentdata ) {
    if( $commentdata['comment_content'] == strtoupper( $commentdata['comment_content'] ))
        $commentdata['comment_content'] = strtolower( $commentdata['comment_content'] );
    return $commentdata;
}
 
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment
 
?>

管理登录后的跳转地址

这个过滤器允许用户在登录后台后自定义跳转地址(不是在后台管理面板中设置),在某些情况下,这个是很有用的。

样例:将订阅者重定向到网站首页

若你不想让用户(订阅者角色 Subscriber)在他们登录后看见后台管理控制面板,即可将登录后的页面重定向到网站首页。

<?php
 
add_filter( 'login_redirect', 'login_redirect_example', 10, 3 );
 
function login_redirect_example( $redirect_to, $request, $user ) {
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'subscriber', $user->roles ) ) {
            return home_url();
        } else {
            return $redirect_to;
            }
    }
    return;
}
 
?>

官方文档中提示我们:必须在is_admin()外使用add_filter函数,因为当调用过滤钩子时函数是无效的。

为插件创建一个动作链接

如果你正在开发一个WordPress插件,那么你可能想要知道其他开发者在插件页面中如何添加一个设置链接的,他们使用的是如下过滤钩子。

样例:在插件页面增加一个设置链接

在插件页面增加一个设置链接,你可以使用下列函数并挂载到过滤钩子。

<?php
 
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'plugin_action_links_example' );
 
function plugin_action_links_example( $links ) {
    $links[] = '<a href="' . get_admin_url( null, 'options-general.php?page=my_plugin_settings' ) . '">' . __( 'Settings' ) . '</a>';
    return $links;
}
 
// Example source: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
 
?>

请注意,我们在这里使用__FILE__ 挂载到函数和过滤器,使用的是插件的名称。

过滤文章编辑器中的内容

你是否想过在后台的编辑器中写文章时候先预填写一些内容,或者需要给作者一些提醒,此时就可以使用过滤钩子the_editor_content。

样例:给文章作者一些提示信息

我们看一下如何给作者提示信息:如果你想给博客作者较多的提示信息,就可以在编辑器中使用如下代码:

<?php
 
add_filter( 'the_editor_content', 'the_editor_content_example' );
 
function the_editor_content_example( $content ) {
    // Only return the filtered content if it's empty
    if ( empty( $content ) ) {
        $template  = 'Hey! Don\'t forget to...' . "\n\n";
        $template .= '<ul><li>Come up with good tags for the post,</li><li>Set the publish time to 08:00 tomorrow morning,</li><li>Change the slug to a SEO-friendly slug,</li><li>And delete this text, hehe.</li></ul>' . "\n\n";
        $template .= 'Bye!';
        return $template;
    } else
        return $content;
}
 
// Example source: http://wpfilte.rs/the_editor_content/
 
?>

用任何你想写的字符串给变量$template赋值。

第二部分结束语

在本文中我们完成了第二部分的10个过滤钩子的学习,希望你喜欢并能学到新知识,请留下您的建议意见,当然也别忘了把你觉得好的文章分享给你的好友哦,下个教程我们再见。

原文出自:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-filters-11-20–cms-21296

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

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

2016-1-19 12:05:17

WordPress开发

50个WordPress过滤钩子(21-30)

2016-1-21 10:13:26

2 条回复 A文章作者 M管理员
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索