
How to Restrict WordPress Pages from Non-Members (BuddyPress Compatible Code Snippet)
| Key Trending Keywords | Focus | 
| WordPress Content Restriction | The main technical practice. | 
| BuddyPress Member-Only Pages | The specific platform and target content type. | 
| Gated Content / Exclusive Content | The monetization/value proposition trend. | 
| Redirect Non-Logged-In Users | The action/functionality. | 
| WordPress Membership Site | The overall solution category. | 
Why Restrict Content? The Power of the Member-Only Area
Building a robust online community using WordPress and BuddyPress requires a strategy for exclusive content. Whether you’re running a paid WordPress membership site, a private forum, or a professional client portal, gated content is essential for creating value and encouraging registrations.
While premium plugins like MemberPress or Restrict Content Pro offer comprehensive solutions, sometimes you need a fast, developer-friendly way to secure specific community pages—such as the main ‘Members’ list or the ‘Activity’ feed—from public access.
This guide provides a precise functions.php snippet to instantly redirect non-logged-in users trying to access your private BuddyPress components.
The Lightweight Method: Protect BuddyPress Pages with a Code Snippet
Instead of installing a heavy content protection plugin for a simple page lock, you can use a small function that hooks into WordPress’s template loading process. This code checks two things: login status and the page slug. If a user is a non-member (logged out) and attempts to view a restricted page, they are instantly redirected to your homepage (or a custom login page).
🚀 The WordPress Content Restriction Code
Place this snippet into your active theme’s functions.php file, or a custom site-specific plugin.
PHP
// Redirect Non-Logged-In Users from Exclusive BuddyPress Pages
function bp_gated_content_redirect() {
    
    // 1. Check if the user is NOT logged in.
    if ( ! is_user_logged_in() ) {
        
        // 2. Define the pages you want to protect (using the page slug or component check).
        // Common BuddyPress pages to restrict: 'members', 'activity' (or 'community').
        if ( is_page( 'members' ) || is_page( 'activity' ) || bp_is_user() ) {
            
            // 3. Perform the redirect to the site's homepage (or your desired login page).
            wp_redirect( home_url() ); 
            exit(); 
        }
    }
}
// Hook this function before the page template loads
add_action( 'template_redirect', 'bp_gated_content_redirect' );
How the Code Protects Your Pages
- ! is_user_logged_in(): This is the core check. It ensures the script only targets public visitors (non-members).
- is_page( 'members' )&- is_page( 'activity' ): These functions target specific WordPress page slugs. You can add or remove pages here (e.g.,- is_page( 'groups' )to protect the Groups directory).
- bp_is_user(): This is a powerful BuddyPress function that targets all individual user profiles, ensuring that people can’t snoop on member profiles without logging in.
- wp_redirect( home_url() );: This immediately sends the unauthorized user to the site’s main URL. You can change- home_url()to any custom login or registration page URL you prefer.
Enhancing Your WordPress Membership Site Security
For a more comprehensive BuddyPress content protection strategy, consider these next steps:
- Custom Redirect URL: Instead of home_url(), usewp_redirect( wp_login_url() )to send users directly to the login screen, boosting conversions.
- User Roles for Granular Access: For advanced content restriction by user role, you would modify the code to check current_user_can('subscriber')or similar roles, allowing you to create different tiers of access for specific groups.
- Explore Dedicated Plugins: If your needs expand to include payment gateways, subscription management, or content dripping, it is highly recommended to migrate to a dedicated membership plugin such as Paid Memberships Pro or Ultimate Member for a fully featured and maintainable system.