0

How to Redirect Buddypress Directories To Login Page For Non Logged in Users?

I'm also using the Yuzify plugin. It gives a code for it, but it's not working on multisite. Can anyone modify this code or provide me with a new code for it?

/**
* Redirect Buddypress Directories To Login Page For Non Logged in Users.
*/
function yzc_redirect_bp_directories_to_login_page() {

    if ( ! is_user_logged_in() && ( bp_is_groups_directory() || bp_is_members_directory() ) ) {
        
        // Get Buddypress Pages;
        $bp_pages = get_option( 'bp-pages' );
        
        // Get Redirection Url
        $redirect_url = youzify_get_login_page_url();
        
        wp_redirect( $redirect_url );
        exit();
    }
}

add_action( 'template_redirect', 'yzc_redirect_bp_directories_to_login_page' );

Thank you.

1
  • what is youzify_get_login_page_url? Have you raised this with Yuzify's support?
    – Tom J Nowell
    Jun 15, 2022 at 16:55

1 Answer 1

0

Try this:

/**
* Redirect Buddypress Directories To Login Page For Non Logged in Users.
*/
function yzc_redirect_bp_directories_to_login_page() {
   if (is_main_site()) {
    if( !is_user_logged_in() && (is_page( 'members' )|| is_page( 'groups' )  || bp_is_group() || bp_is_user()  ) ) { 
         // Get Redirection Url
         if ( function_exists( 'youzify_get_login_page_url' )) {
            $redirect_url = youzify_get_login_page_url();
        }
        else {
            $redirect_url=wp_login_url( get_permalink() );
        }
        
        wp_redirect( $redirect_url );
        exit(); 
    }
  }
} 

add_action( 'template_redirect', 'yzc_redirect_bp_directories_to_login_page' );
1
  • Please also explain why your code should work, instead of just giving the CODE as an answer. Best Regards.
    – Fayaz
    Sep 5, 2022 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.