wordpress 过滤器钩子add_filter作用 和 例子

























boj_add_site_name_to_title() 函数修改 $title 参数并返回给 WordPress。$sep 参数在函数中使用,但没有返回。 过滤器钩子函数 
除了前面提到的 apply_filters() 和 add_filter() 函数,WordPress 还提供其他的操作过滤器钩子的函数。 
apply_filters_ref_array 
类似于动作钩子里面的 do_action_ref_array() 函数。 
<?php 
apply_filters_ref_array( $tag, $args ); ?> 
假设你要执行一个一般的 WordPress 没有的复杂的数据库查询来加载首页的 posts。WordPress 提供了一个叫做 posts_results 的过滤器钩子使得你可以改变它。下面是 WordPress 核心中的代码: 
<?php 
$this -> posts = apply_filters_ref_array( 
'posts_results', array( $this -> posts, & $this ) ); ?> 
这个过滤器钩子向所有注册到它的过滤器传递一个 post 对象的数组。下面的例子,你完全重写这个 post 对象的数组并用自定义的内容代替。默认情况下,WordPress 查询 post 类型的 posts。下面改成查询 page 类型的 psots 并显示在首页。 
这段代码使用了 wpdb 类,在 part-6 “插件安全” 中将详细介绍。 
<?php 
add_filter( 'posts_result', 'boj_custom_home_page_posts' );  
function boj_cumstom_home_page_posts( $result ) { 












global $wpdb, $wp_query; /* 检查是否在首页 */ if ( is_home() ) { /* 每页的 post 个数 */ 
$per_page = get_option( 'posts_per_page' ); /* 得到当前页 */ 
$paged = get_query_var( 'paged' ); /* 设置 $page 变量 */ 
$page = ( ( 0 == $paged || 1 == $paged ) ? 1 : absint( $paged ) ); /* 设置偏移的 posts 的个数 */ 
$offset = ( $page - 1 ) * $per_page. ','; 
/* 通过 $offset 和 要显示的 posts 数量来设置 limit */ $limits = 'LIMIT'. $offset . $per_page; /* 从数据库查询结果 */ 
$result = $wpdb -> get_results(" 
SELECT SQL_CALC_FOUND_ROWS $wpdb -> posts. * FROM $wpdb -> posts WHERE 1 = 1 
AND post_type = 'page' AND post_status = 'publish' ORDER BY post_title ASC $limits " ); } 
return $result; } 












?> 
remove_filter 
<?php 
remove_filter( $tag, $function_to_remove, $priority, $accepted_args ); ?> 
这和前面的 remove_action 类似。 
下面看看 WordPress 定义在 wp-includes/default-filters.php 页面中的默认的过滤器。其中一个有意思的过滤器叫做 wpautop(),它将双换行转换成 HTML 的 <p> &lt/p>。这也就是我们在 HTML 发布内容时,直接回车便可在最终前端显示的时候换行的原因。它在核心代码中的几个钩子中都执行。下面是其中一个实例: 
<?php 
add_filter( 'the_content', 'wpautop' ); ?> 
这将 wpautop() 过滤器应用到 post 的内容中,把每个换行都转换成段落( <p> )。但是也许有的客户不希望他的内容自动变成段落。那么你就可以使用 remove_filter() 函数从 the_content 钩子中删除这个过滤器。 
<?php 
remove_filter( 'the_content', 'wpautop' ); ?> 
因为在 add_filter 的时候没有定义优先级,所以这里也不需要。 remove_all_filters 
和前面的remove_all_actions类似。 
<?php 












remove_all_filters( $tag, $priority ); ?> 
has_filter 
和前面的 has_action 类似。 current_filter 
同样类似于 did_action。不过它不仅仅对过滤器钩子有效,同样对动作钩子也有效,所以它返回的是当前的 action 或者 filter 钩子。这个函数在你对多个钩子使用单个函数,但是需要依赖不同的钩子执行不同的内容的时候非常的有用。例如,客户希望在 post 标题 和内容中限制一些内容,但是这两个限制的minganci的集合是不同的。使用 current_filter() 函数来根据钩子设置不同的minganci表就可以实现用一个函数同时过滤 the_content 和 the_title。使用下面的代码,你可以把minganci替换成**。 
<?php 
add_filter( 'the_content', 'boj_replace_unwanted_words' ); add_filter( 'the_title', 'boj_replace_unwanted_words' );  
function boj_replace_unwanted_words( $text ) { /* 如果过滤器钩子是 the_content */ 
if( 'the_content' == current_filter() ) $words = array( 'min', 'gan', 'ci' ); /* 如果钩子是 the_title */ 
elseif( 'the_title' == current_filter() ) 
$words = array( 'zhen', 'de', 'hen', 'min', 'gan' ); /* 替换minganci */ 
$text = str_replace( $words, '**', $text ); return $text; } 












?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值