【译】WordPress 中的50个过滤器(6):第41-50个过滤器

【译】WordPress 中的50个过滤器(6):第41-50个过滤器

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

即将吹响终点的号角!翻译得好累,如果你看不惯俺的渣渣翻译,无比欢迎来做修正工作!下面我们看看最后的十个过滤器。

本系列文章翻译自tutsplus,原作者为Barış Ünver,翻译人:Jeff,转载请注明原始来源及翻译人,谢谢!

本文若有修正,不会更新于本页,只会更新到Github项目地址上。

迫不及待?开始吧!

过滤脚本文件资源

WordPress 有自己的脚本文件加载方式,wp_enqueue_script()这个函数让我们注册一个js文件而非硬编码方式引入,而script_loader_src这个过滤器可以让我们处理脚本文件加载及输出的方式。

例子:移除脚本文件的版本号

诸如Google Page Speed 或Yahoo YSlow 这种网页速度评测工具都非常讨厌url中得版本号参数。当然,版本号参数的存在是有很大的意义的——可以让浏览器处理好缓存与更新静态资源的关系。如果你不喜欢WordPress 加载的静态文件会自动添加版本号参数的做饭,你可以用下面的代码删除之:

<?php function script_loader_src_example( $src ) {
 return remove_query_arg( 'ver', $src ); 
 } 
 
 add_filter( 'script_loader_src', 'script_loader_src_example' );
 // Tiny bonus: You can do it with styles, too! 
 add_filter( 'style_loader_src', 'script_loader_src_example' ); 
 // Example source: http://www.wpmayor.com/15-practical-ways-boost-wordpress-speed/ 
?>

添加HTML到特色图像Metabox

正如函数名称所言,admin_post_thumbnail_html是一个可以让你在特色图像区域插入HTML 的函数。插入的HTML 会显示在“设置特色图像”文字下面。

例子:提醒你的用户正确设置特色图像

<?php add_filter( 'admin_post_thumbnail_html', 'admin_post_thumbnail_html_example' );   
function admin_post_thumbnail_html_example( $html ) { 
return $html .= '<p>Hi Mr. Smith! Click above to add an image to be displayed at the top of your post. Remember: <strong>The width of the image should be at least 900px</strong>!</p>'; 
} 
 
?>

控制灌水评论攻击

默认的,为了防止灌水式评论,WordPress会采取一些措施。比如说,如果你的访客已经发表了一条评论了,再次发表必须等待15秒。下面的过滤器可以让你设置这个时间区间或者说移除WordPress 的这个机制。

例子:让访客评论间隔更长一点

下面的代码设置为60秒的连续评论间隔时间差。

<?php add_filter( 'comment_flood_filter', 'comment_flood_filter_example', 10, 3 );  
function comment_flood_filter_example( $flood_control, $time_last, $time_new ) { 
$seconds = 60; 
if ( ( $time_new - $time_last ) < $seconds )
 return true; 
 return false;
  } 
 // Example source: http://codex.wordpress.orgFAQ_Working_with_WordPress#How_do_I_prevent_comment_flooding.3F 
 
 ?>

例子:移除连续评论间隔时间机制

<?php 
 
remove_all_filters( 'comment_flood_filter' );   
add_filter( 'comment_flood_filter', '__return_false', 10, 3 );
 
 ?>

修改“概览”部分的栏目

WordPress 的“概览”栏目可以让你知道总体的文章数量,页面数量,评论数量等。dashboard_glance_items这个过滤器帮助我们处理更多的信息展示。

例子:在“概览”栏目显示自定义文章类型

假设你本身有 event 这个自定义文章类型。如果要显示在“概览”中,你可以使用下面的代码:

<?php
 
add_filter( 'dashboard_glance_items', 'dashboard_glance_items_example' );
 
function dashboard_glance_items_example( $items = array() ) {
$post_types = array( 'event' );
foreach( $post_types as $type ) {
    if( ! post_type_exists( $type ) ) continue;
    $num_posts = wp_count_posts( $type );
    if( $num_posts ) {
        $published = intval( $num_posts->publish );
        $post_type = get_post_type_object( $type );
        $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'your_textdomain' );
        $text = sprintf( $text, number_format_i18n( $published ) );
        if ( current_user_can( $post_type->cap->edit_posts ) ) {
        $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $text . '</a>';
            echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        } else {
        $output = '<span>' . $text . '</span>';
            echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        }
    }
}
return $items;
}
 
// Example source: http://www.trickspanda.com/2014/03/add-custom-post-types-glance-dashboard-widget-wordpress/
 
?>

很容易不是吗?其他自定义文章类型的话只需要修改$post_types这个参数即可。

修改默认的登录表单信息

login_message这个过滤器可以让我们自定义默认的输出信息(注意非为报错信息)

例子:修改默认的丢失密码信息

<?php
 
add_filter( 'login_message', 'login_message_example' );
 
function login_message_example( $message ) {
$action = $_REQUEST['action'];
if( $action == 'lostpassword' ) {
    $message = '<p class="message">Enter your email address, then check your inbox for the "reset password" link!</p>';
    return $message;
}
return;
}
 
// Example source: http://www.pypelineweb.com/blog/change-wordpress-login-message-filter-it-out/
 
?>

WordPress 登录中的其他action如下:

logout、lostpassword或retreivepassword、resetpass或rp、register、login

修改大范围的更新信息

当你更新、删除或移动文章到垃圾箱的时候会有很多提示信息,如果你想批量修改这些信息,bulk_post_updated_messages过滤器将会派上用场。

例子:修改自定义文章类型的提示文字

延伸之前的例子,还是event 这个自定义文章类型:

<?php
 
add_filter( 'bulk_post_updated_messages', 'bulk_post_updated_messages_example', 10, 2 );
 
function bulk_post_updated_messages_example( $bulk_messages, $bulk_counts ) {
$bulk_messages['event'] = array(
    'updated'   => _n( '%s event updated.', '%s events updated.', $bulk_counts['updated'] ),
    'locked'    => _n( '%s event not updated, somebody is editing it.', '%s events not updated, somebody is editing them.', $bulk_counts['locked'] ),
    'deleted'   => _n( '%s event permanently deleted.', '%s events permanently deleted.', $bulk_counts['deleted'] ),
    'trashed'   => _n( '%s event moved to the Trash.', '%s events moved to the Trash.', $bulk_counts['trashed'] ),
    'untrashed' => _n( '%s event restored from the Trash.', '%s events restored from the Trash.', $bulk_counts['untrashed'] ),
);
 
return $bulk_messages;
}
 
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/bulk_post_updated_messages
 
?>

过滤默认的分类小工具

在某些情况下,你可能需要修改分类小工具,借助widget_categories_args 这个过滤器,你就可以实现。

例子:排除某些分类

<?php
 
add_filter( 'widget_categories_args', 'widget_categories_args_example' );
 
function widget_categories_args_example( $cat_args ) {
$exclude_arr = array( 4, 10 );
 
if( isset( $cat_args['exclude'] ) && !empty( $cat_args['exclude'] ) )
    $exclude_arr = array_unique( array_merge( explode( ',', $cat_args['exclude'] ), $exclude_arr ) );
$cat_args['exclude'] = implode( ',', $exclude_arr );
return $cat_args;
}
 
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/widget_categories_args
 
?>

修改$exclude_arr这个参数,相关id为分类id。

当用户注册成功时候重定向

相关过滤器为registration_redirect

例子:让新用户下载你的电子书

如果你是提供电子书给你的注册用户,那么你可以让他们注册后就跳转到有下载链接的页面,类似下面的代码:

<?php
 
add_filter( 'registration_redirect', 'registration_redirect_example' );
 
function registration_redirect_example() {
return home_url( '/your-free-ebook/' ); 
}
 
// Example source: http://wpsnipp.com/index.php/functions-php/redirect-a-successful-registration-page/
 
?>

值得注意的是,通过wp_safe_redirect()函数设置的跳转链接不能为外链,除非你通过allowed_redirect_hosts函数添加域名白名单(这个之前在某篇文章中已经说过的了)

更改评论表单的域

WordPress 中comment_form()使用展示评论表单,下面的例子让你可以自定义之:

例子:移除表单的url 域

<?php
 
add_filter( 'comment_form_default_fields', 'comment_form_default_fields_example' );
 
function comment_form_default_fields_example( $fields ) {
unset( $fields['url'] );
return $fields;
}
 
// Example source: http://wpsnipp.com/index.php/comment/remove-unset-url-field-from-comment-form/
 
?>

设置允许的可接受上传文件类型

默认你可以上传多种文件类型到WordPress 的多媒体中,通过upload_mime过滤器,你可以对文件类型进行控制。

例子:禁止上传gif 格式图片

<?php
 
add_filter( 'upload_mimes', 'upload_mimes_example' );
 
function upload_mimes_example( $existing_mimes = array() ) {
unset( $existing_mimes['gif'] );
return $existing_mimes;
}
 
// Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/upload_mimes
 
?>

总结

译者:翻译到这里,终于宣告结束了!!!这个系列文章从打算开始翻译到今天初步翻译完成已经过了差不多一个月,期间也想过放弃,但一旦开始做了总不能烂尾吧?抱着善始善终的原则,勉勉强强翻译完了。翻译过程中也学到不少,但平心而论,翻译水平确实不行——所以如果你看不顺眼这“垃圾”翻译,尽管来修正吧!

评分:
当前平均分 0.00 (0%) - 0 个投票
查看 评论
  1. 这里的主题真漂亮 有免费的 好的主题没

    9 年前 回复
发表评论