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 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路