1

I'm trying to prevent moderation of forum posts for users with the 'moderate' capability OR an active 'premium' membership plan (WC Memberships).

I want replies from such authors to not be subject to our moderation rules (number of links, blocklisted words, etc.).

I've tried using the following filters with no luck:

add_filter( 'bbp_bypass_check_for_moderation', 'bbp_bypass_if_user_can_moderate', 10, 4 );

function bbp_bypass_if_user_can_moderate( $anonymous_data, $author_id, $title, $content ){
    if( user_can( $author_id, 'moderate' ) || wc_memberships_is_user_active_member( $author_id, 'premium' ) ){
        return true;
    } else {
        return false;
    }
}

add_filter( 'bp_bypass_check_for_moderation', 'bp_bypass_if_user_can_moderate', 10, 3 );

function bp_bypass_if_user_can_moderate( $user_id, $title, $content ){
    if( user_can( $user_id, 'moderate' ) || wc_memberships_is_user_active_member( $user_id, 'premium' ) ){
        return true;
    } else {
        return false;
    }
}

It seems like the above code should work. Am I overlooking something?

Reference:

  • https://www.buddyboss.com/resources/reference/functions/bbp_check_for_moderation/
  • https://www.buddyboss.com/resources/reference/hooks/bp_bypass_check_for_moderation/
  • https://developer.wordpress.org/reference/functions/user_can/
  • https://docs.woocommerce.com/document/woocommerce-memberships-function-reference/#wc_memberships_is_user_active_member

2 Answers 2

1

You've got the bp_bypass_if_user_can_moderate arguments wrong. From the documentation you linked to it should be bool $value, int $user_id, string $title, string $content, and as normal for filters you should pass through $value if you're not going to change it. e.g. here's a fixed up version of your filter:

add_filter( 'bp_bypass_check_for_moderation',
            'bbp_bypass_if_user_can_moderate', 10, 4 );

function bbp_bypass_if_user_can_moderate( $value, $user_id, $title, $content ) {
    if ( ( !$value ) && ( user_can( $user_id, 'moderate' ) ||
         wc_memberships_is_user_active_member( $user_id, 'premium' ) ) ) {
        return true;
    }

    return $value;
}

I can't say for sure this will work though.

3
  • Thanks. Your code made me realize that I omitted an argument for bp_bypass_if_user_can_moderate, but I think you've conflated the two (very similar) functions/filters... bbp_bypass_check_for_moderation (bbpress related) and bp_bypass_check_for_moderation (buddypress related). I'm still unable to get the code to work :( Commented Aug 20, 2021 at 15:31
  • Fair enough - I was just looking at the bbpress plugin and didn't realise the really was a bbp_ filter too.
    – Rup
    Commented Aug 24, 2021 at 15:18
  • Not a problem. Appreciate you taking the time to help! Commented Aug 24, 2021 at 15:19
1

After much trial and error, I was able to figure it out.

The documentation for bbp_bypass_check_for_moderation is missing the first parameter ($value). Here is the working code with all parameters:

/* Bypass moderation (bbPress) */
add_filter( 'bbp_bypass_check_for_moderation', 'bbp_bypass_if_user_can_moderate', 5, 5 );
function bbp_bypass_if_user_can_moderate( $value, $anonymous_data, $author_id, $title, $content ){
    if( user_can( $author_id, 'moderate' ) || wc_memberships_is_user_active_member( $author_id, 'premium' ) ){
        return true;
    } 
    return false;
}

/* Bypass moderation (BuddyPress) */
add_filter( 'bp_bypass_check_for_moderation', 'bp_bypass_if_user_can_moderate', 5, 4 );
function bp_bypass_if_user_can_moderate( $value, $user_id, $title, $content ){
    if( user_can( $author_id, 'moderate' ) || wc_memberships_is_user_active_member( $author_id, 'premium' ) ){
        return true;
    } 
    return false;
}
1
  • 1
    I think you should still return $value not false in the case you're not changing it, in case there are any other filters in the chain.
    – Rup
    Commented Aug 24, 2021 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.