前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【译】WordPress 中的50个过滤器(3):第11-20个过滤器

【译】WordPress 中的50个过滤器(3):第11-20个过滤器

作者头像
Jeff
发布2018-01-22 10:21:34
8680
发布2018-01-22 10:21:34
举报
文章被收录于专栏:DeveWork

本文为系列第三篇,原文:50 Filters of WordPress: Filters 11-20 原文地址

不多说,直接进入正题。

本系列文章翻译自tutsplus,原作者为Barış Ünver,翻译人:Jeff,转载请注明原始来源及翻译人,谢谢! 本文若有修正,不会更新于本页,只会更新到Github项目地址上。

在WordPress 中使用可翻译的数据

WordPress 的有一点强大之处就是几乎每一句语言都可以被翻译。如果你的网站语言是英语,你可能就没有这个需求;但其它语系的客户呢?

gettext这个过滤器可以让你在WordPress 中轻松玩转可翻译的数据。让我们看一个例子:

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

假设你找到了个非常nice 的插件,但你发现插件的开发者的英语水平实在是不敢恭维,那么你会在代码中看到很多的错误的文本代码。好在所有的参数都是可以被翻译的,那么你就可以用下面的代码借助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 会使用一个名为的函数去优化标题:比如说自动替换空格为“-”并拟保存为url别名。通过sanitize_title你可以扩展这个函数。

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

<?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 中排除短代码(?)

这个便捷的过滤器可以让你指定哪个短代码不经过函数运行,详情见官方文档

例子:从Texturization 中排除短代码

如果你想要某个短代码从Texturization 中排除,你可以添加类似的代码:

<?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这个过滤器可以让我们做一个调整。

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

<?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_url'为 'comment_author'即可。

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

你是否知道你可以通过邮件在你的WordPress 上发表文章?WordPress 提供了这个功能,相关函数叫enable_post_by_email_configuration

例子:关闭“通过邮箱发文章”功能

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

过滤页面标题

WordPress 中通过wp_title()函数输出页面标题——就是你在浏览器标签栏上看到的那个。

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

这篇文章解释了如何自定义页面标题,详细你可以查看全文,这里直接给出个核心的例子:

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

管理登录跳转地址

这个过滤器允许你的用户在登录后台后自定义跳转地址。

例子:订阅用户登录后自动跳转到网站首页

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

官方codex 中提醒我们一点:确保你是在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)   ?>

过滤编辑器中得文本内容

是否想在后台的编辑器上预填充一些内容,或者需要给你的编辑人员一些提醒?如果有这类需求,那么你可以使用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 参数的内容为你喜欢的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014/11/22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在WordPress 中使用可翻译的数据
    • 例子:更正开发者的语法错误
    • 优化标题url别名
      • 例子:移除标题url别名中的“the”单词
      • 从Texturization 中排除短代码(?)
        • 例子:从Texturization 中排除短代码
        • 过滤评论的批准状态
          • 例子:标记长用户名的评论为垃圾评论
          • 配置“通过邮箱发文章”功能属性
            • 例子:关闭“通过邮箱发文章”功能
            • 过滤页面标题
              • 例子:重写页面标题(正确的方式)
              • 在评论被保存到数据库前进行一些操作
                • 例子:评论自动转化大写文字为小写
                • 管理登录跳转地址
                  • 例子:订阅用户登录后自动跳转到网站首页
                  • 为插件创建一个设置链接
                    • 添加一个设置链接展示在插件页面
                    • 过滤编辑器中得文本内容
                      • 例子:编辑提醒
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档