【译】WordPress 中的50个过滤器(3):第11-20个过滤器
本文为系列第三篇,原文: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...' . "nn"; $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>' . "nn"; $template .= 'Bye!'; return $template; } else return $content; } // Example source: http://wpfilte.rs/the_editor_content/ ?> |
---|
修改上面$template
参数的内容为你喜欢的。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 视频配音篇,如何使用百度翻译将文本转换为mp3语音?
- Objective-C的hook方案/ Method Swizzling
- Python爬虫:无账号无限制获取企查查信息
- Spark Streaming——Spark第一代实时计算引擎
- bamtools分割bam文件
- 在genome browser中添加自己的注释文件
- 原理+代码|Python基于主成分分析的客户信贷评级实战
- Python办公自动化 | 从PPT到Word
- linux命令行参数getopts参数二选一
- 黎巴嫩首都爆炸能量有多大?物理学家看视频计算:300吨TNT!
- OracleDG 环境主备业务数据不同步备库报ORA-600错误的处理过程
- Xcode清理模拟器文件
- 【Android 音视频开发打怪升级:FFmpeg音视频编解码篇】六、FFmpeg简单合成MP4:视屏解封与重新封装
- 32.opengl高级光照-延迟着色法
- Tsunami:一款功能强大的通用网络安全扫描工具