Custom Login Page with BuddyBoss – BuddyX Theme

Protecting Your BuddyX Community with Custom Login Page

This guide will help you secure your BuddyX community by ensuring that only logged-in users can access it, and it will guide you to use a custom login page. We’ll also explain how to place the login link at the top of your site.

Steps to Protect Your BuddyX Community

1. Redirect Non-Logged-In Users to a Custom Login Page

To force non-logged-in users to access a custom login page, follow these steps:

Code Snippet:

Add the following code snippet to your theme’s functions.php file or a custom plugin to protect your entire community.

// Hook into the 'bp_core_no_access' filter to modify the redirect.
add_filter( 'bp_core_no_access', 'wbcom_custom_no_access_redirect_to_login', 10, 1 );

function wbcom_custom_no_access_redirect_to_login( $args ) {
    // Define the custom login page URL.
    $custom_login_page = home_url( '/login/' );

    // Get the current requested URL (for redirection after login).
    $requested_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    // Modify the arguments to use the custom login page instead of wp-login.php.
    $args['root'] = $custom_login_page;
    $args['redirect'] = add_query_arg( 'redirect_to', urlencode( $requested_url ), $custom_login_page );

    return $args;
}

// Force custom redirect for all BuddyBoss no-access redirects.
add_filter( 'bp_no_access_redirect', 'wbcom_override_no_access_redirect', 10, 1 );

function wbcom_override_no_access_redirect( $redirect ) {
    // Define the custom login page.
    $custom_login_page = home_url( '/login/' );

    // Return the custom login page as the redirect URL.
    return $custom_login_page;
}

// Optionally, override wp-login.php requests.
add_action( 'init', 'wbcom_custom_redirect_wp_login' );

function wbcom_custom_redirect_wp_login() {
    // Redirect wp-login.php to the custom login page.
    $custom_login_page = home_url( '/login/' );

    // Check if the current page is wp-login.php and not performing any allowed action.
    if ( strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false && !is_user_logged_in() ) {
        wp_redirect( $custom_login_page );
        exit();
    }
}

This code ensures that non-logged-in users are redirected to the custom login page, preventing access to the community.

2. Displaying the Login Link at the Top

To make sure the login page link is prominently displayed, you can add a login link to your site’s header or menu.

Steps:

  1. Navigate to Appearance > Menus in your WordPress dashboard.
  2. Create a new menu item for the login page.
    • URL: https://yoursite.com/login/
    • Link Text: “Login”
  3. Assign the menu to your site’s header menu or wherever you’d like the login link to appear.
  4. Save the changes.

By following the steps in this guide, you have now protected your BuddyX community and added a custom login page link for easier access by your users.

Last updated: April 14, 2025