bbPress: Forums for Logged-In Members Only
Sends logged-out visitors who open any forum, topic or forum profile to the login screen, making the whole bbPress forum private.
Opens WPIDE on
Code
if ( ! function_exists( 'is_bbpress' ) ) {
return;
}
add_action( 'template_redirect', function () {
if ( is_user_logged_in() || ! is_bbpress() ) {
return;
}
wp_safe_redirect( wp_login_url( home_url( add_query_arg( array() ) ) ) );
exit;
} );
add_action( 'pre_get_posts', function ( $query ) {
if ( is_user_logged_in() || ! $query->is_feed() || ! $query->is_main_query() ) {
return;
}
$types = (array) $query->get( 'post_type' );
if ( array_intersect( $types, array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) {
$query->set( 'post__in', array( 0 ) );
}
} );
Notes
Forum content is also kept out of feeds for visitors.
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.