Halloween Sale 15% off Ends in See the plans
PHP

bbPress: Only Staff Can Start Topics in Announcement Forums

Stops regular members from creating new topics in chosen forums, such as Announcements, while still letting them reply.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! function_exists( 'bbp_get_forum_id' ) ) {
    return;
}

// Forum IDs where only staff start topics.
$wpide_bbp_staff_forums  = array( 123 );
$wpide_bbp_allow_replies = true;

add_filter( 'bbp_current_user_can_access_create_topic_form', function ( $can ) use ( $wpide_bbp_staff_forums ) {
    if ( current_user_can( 'moderate' ) ) {
        return $can;
    }

    return in_array( (int) bbp_get_forum_id(), $wpide_bbp_staff_forums, true ) ? false : $can;
} );

add_filter( 'bbp_current_user_can_access_create_reply_form', function ( $can ) use ( $wpide_bbp_staff_forums, $wpide_bbp_allow_replies ) {
    if ( $wpide_bbp_allow_replies || current_user_can( 'moderate' ) ) {
        return $can;
    }

    return in_array( (int) bbp_get_topic_forum_id( bbp_get_topic_id() ), $wpide_bbp_staff_forums, true ) ? false : $can;
} );

// Also refuse topics posted to those forums directly.
add_filter( 'bbp_new_topic_pre_content', function ( $content ) use ( $wpide_bbp_staff_forums ) {
    $forum_id = isset( $_POST['bbp_forum_id'] ) ? absint( $_POST['bbp_forum_id'] ) : 0;

    if ( ! current_user_can( 'moderate' ) && in_array( $forum_id, $wpide_bbp_staff_forums, true ) ) {
        bbp_add_error( 'wpide_bbp_staff_only', '<strong>Error:</strong> Only staff can start topics in this forum.' );
    }

    return $content;
} );

Notes

Staff means anyone with bbPress moderator rights. Set $wpide_bbp_allow_replies to false to block replies too.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.