Php 如何创建一个;添加“U过滤器”;函数if";“过滤器名称”;是以“的形式”$“过滤器”;在应用过滤器?

Php 如何创建一个;添加“U过滤器”;函数if";“过滤器名称”;是以“的形式”$“过滤器”;在应用过滤器?,php,wordpress,bbpress,Php,Wordpress,Bbpress,在以下WP php代码中: 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 ===

在以下WP php代码中:

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 );
    }
我想通过使用过滤器更改“$replays”。由于上面有“应用过滤器”,我认为可以添加“添加过滤器”。但过滤器名称似乎是“$filter”

在这种情况下,如何创建“添加过滤器”函数


谢谢您的帮助。

基于方法第二个参数
$integer
的过滤器名称
bbp\u get\u topic\u post\u count\u int
(当
$integer
true时)或
bbp\u get\u topic\u post\u count
(当
$integer
false
时,如果在方法调用时没有该值,则为
$integer
的默认参数值)

$filter
的值在此处分配:

$filter = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';
因此,您不需要修改该方法,但应该搜索该方法的用法,以查看给定了$integer的哪个输入参数

要对$integer=true使用筛选器,请使用:

add_filter( 'bbp_get_topic_post_count_int', 'bbp_get_topic_post_count', 10, 2 );
add_filter( 'bbp_get_topic_post_count', 'bbp_get_topic_post_count', 10, 2 );
要将筛选器用于$integer=false,请使用:

add_filter( 'bbp_get_topic_post_count_int', 'bbp_get_topic_post_count', 10, 2 );
add_filter( 'bbp_get_topic_post_count', 'bbp_get_topic_post_count', 10, 2 );
我解决了这个问题。(在我的问题代码中,只需将过滤器名称更正为“bbp_get_topic_post_count”),再次感谢您的友好回答。