drnmslpz42661 2015-07-13 20:47
浏览 31
已采纳

如果“过滤器名称”在apply_filters中以“$ filter”的形式创建“add_filter”函数?

In the following WP php code:

function bbp_get_topic_post_count( $topic_id = 0, $integer = false ) {
        $topic_id = bbp_get_topic_id( $topic_id );
        $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ) + 1;
        $filter   = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';

        return apply_filters( $filter, $replies, $topic_id );
    }

I would like to change "$replies" by using filters. Since there is "apply_filters" above, I think it's possible to add "add_filter". But it seems that the filter name is "$filter"

function bbp_reply_count_modified( $replies, $topic_id ) {
            $topic_id = bbp_get_topic_id( $topic_id );
            $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ); // deleted '+ 1'
            return $replies;
add_filter( '___________________', 'bbp_reply_count_modified', 10, 2 );

In this case, how can I create an "add_filter" function?

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • doumibi6899 2015-07-13 21:33
    关注

    The filter name, based on the methods second parameter $integer, is either bbp_get_topic_post_count_int (when $integer is true) or bbp_get_topic_post_count (when $integer is false, which is the default parameter value of $integer, if there isn't a value for it at method call).

    The value of $filter is assigned here:

    $filter = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';
    

    Therefore, you don't need to modify the method, but should search for usage of the method to see which input parameter for $integer is given.

    To use the filter for $integer = true, use:

    add_filter( 'bbp_get_topic_post_count_int', 'bbp_get_topic_post_count', 10, 2 );
    

    To use the filter for $integer = false, use:

    add_filter( 'bbp_get_topic_post_count', 'bbp_get_topic_post_count', 10, 2 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大