bbPress: Stop Replies to Old, Inactive Topics
Hides the reply form on topics with no activity for a set number of days and shows a note instead, which prevents necro-posting and old-thread spam.
Opens WPIDE on
Code
if ( ! function_exists( 'bbp_get_topic_id' ) ) {
return;
}
// Days without activity before replies stop.
$wpide_bbp_lock_days = 180;
$wpide_bbp_is_stale = function ( $topic_id ) use ( $wpide_bbp_lock_days ) {
$last = get_post_meta( $topic_id, '_bbp_last_active_time', true );
return $last && time() - strtotime( $last . ' UTC' ) > $wpide_bbp_lock_days * DAY_IN_SECONDS;
};
add_filter( 'bbp_current_user_can_access_create_reply_form', function ( $can ) use ( $wpide_bbp_is_stale ) {
if ( ! $can || current_user_can( 'moderate' ) || ! bbp_is_single_topic() ) {
return $can;
}
return $wpide_bbp_is_stale( bbp_get_topic_id() ) ? false : $can;
} );
add_action( 'bbp_template_after_single_topic', function () use ( $wpide_bbp_is_stale ) {
if ( ! current_user_can( 'moderate' ) && $wpide_bbp_is_stale( bbp_get_topic_id() ) ) {
echo '<div class="bbp-template-notice"><ul><li>This topic has been quiet for a long time, so replies are closed. Feel free to start a new topic.</li></ul></div>';
}
} );
Notes
Topics are not changed in the database; the reply form simply stops showing. Moderators can still reply.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.