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

50个WordPress过滤钩子(41-50)

在本系列的前面几个部分,我们已经学习了 40 个WordPress过滤钩子,下来我们继续学习所选的50个钩子的最后10个。

过滤脚本文件资源

WordPress 有自己的脚本文件加载方式,函数wp_enqueue_script()可以加载js文件而非硬编码方式引入,过滤钩子script_loader_src则处理脚本文件加载及输出的方式。

样例:从脚本文件中移除版本参数

Google Page Speed 或Yahoo YSlow 等网站性能检测工具都很讨厌JavaScript 文件中的url版本参数。当然,版本参数的存在是有一定道理的:URLs中的脚本文件版本参数不会保存在代理服务器缓存中 (详见:more info on this topic here),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/
 
?>

就这样,脚本和CSS文件就不会再有任何的版本参数信息了。

如果你想学习更多的WordPress中的脚本知识,可参考http://code.tutsplus.com/articles/the-complete-guide-to-proper-javascript-usage-with-wordpres

增加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>';
 
}
 
?>

有了这些帮助提示信息,他就再也不会忘记该如何设置最小宽度为900 pixels的特色图像了

使用钩子函数comment_flood_filter防范灌水评论攻击

WordPress一般会禁止访客的灌水式评论。例如:默认设置下,访客发表两篇评论的间隔时间必须超过15秒。下面的过滤钩子允许你改变这个时间间隔或者直接移除WordPress 评论审核机制。

样例:加大允许访客评论的时间间隔

如上文所述,WordPress禁止访客在15秒内连续评论,但是某些情况下,我们需要修改此时间间隔,比如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.org/FAQ_Working_with_WordPress#How_do_I_prevent_comment_flooding.3F
 
?>

可以将代码中的60秒改为你需要的时间。

样例:移除连续评论间隔时间审核机制

如果你不介意访客的灌水评论,可以直接使用下面两行代码将评论审核机制移除。

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

注意到remove_all_filters()函数了吧,顾名思义,它可以移除评论审核机制。

修改“概览”部分的栏目

WordPress 的“概览”栏目可以让我们知道网站数据库中文章、页面和评论总体数量。过滤钩子dashboard_glance_items可以让其显示更多信息,比如指定分类的文章数。

样例:在“概览”栏目显示Events文章类型

例如有个“event blog”博客站点,有个名为“event ”的自定义文章类型,访客可据此了解当地发生的事件消息,为了统计博客中事件信息总数概况,便可以使用下面的函数并hook到dashboard_glance_items filter过滤钩子。

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

在文件wp-login.php中有以下的action信息:

  • logout
  • lostpassword and retreivepassword (alias)
  • resetpass and rp (alias)
  • register
  • login

就像上面的样例,我们也可以为其他的action自定义不同的提示信息。

修改批量更新信息

当你更新、移动文章到回收站或者删除文章时有很多提示信息,若想批量修改这些信息,可以使用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即可。

当用户注册成功后页面重定向

一般情况下,注册成功后WordPress只提醒用户核对邮箱信息,并不会重定向到需要的页面,然而借助于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 字段

例如你是一个自由撰稿网站的设计者,正在开发一款插件,为客户个人信息安全考虑,需要将网站所有评论表单中的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 格式图片

从1999年到2014年,gif图片格式一直都很受欢迎,记得当时出现的第一张gif图片是个跳舞的小人,在创建Yahuoo首页的纯真年代,地球村的人们似乎都很享受当在ICQ上有朋友给我们发即时信息的恼人的提示声….

有时候不想要这些喧闹时,我们就可以禁止用户上传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
 
?>

第五部分结语

在本文中我们学习了50个过滤钩子的最后10个,希望你喜欢并能从中学到新知识,下一个章节我们将重新快速浏览学过的过滤钩子,到时再见。

请在下方的留言区写下您的建议意见,如果喜欢本文,别忘了分享给你的好友。

原文出自:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-filters-41-50–cms-21299

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

您已阅读完《50个 WordPress 过滤钩子(共7篇)》专题的第 6 篇。请继续阅读该专题下面的文章:

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

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

50个WordPress过滤钩子(31-40)

2016-1-21 10:31:26

WordPress开发

50个WordPress过滤钩子(总结)

2016-1-21 10:47:13

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索