Redirecting logged out users from specific pages

I have a wordpress website, its using buddypress and bbpress. I need to hide/redirect all the buddypress and bbpress pages from people who are not logged in. So if someone lands on the members page, profile page or any forum topic it needs to redirect them to the signup page.

I tried maybe 5 plugins, all of them caused issues like 404 errors, not working or just white pages.

Read More

The url structure is like this:

www.example.com/members
www.example.com/members/luke
www.example.com/forums
www.example.com/forums/forum/general-chat

Does anyone know how I can do this without a plugin?

Related posts

2 comments

  1. you have to modify from within a child theme the profile-loop.php file

    your-child-theme/members/single/profile/profile-loop.php
    

    On the first line of the file, add

    <?php if ( is_user_logged_in()  ) : ?>
    

    At the end of the file, insert between the last endif and the last do_action this:

    <?php else : ?>
    <?php echo “<div style=’width: 600px;height:25px; padding: 4px; border: 3px solid #ff0000; text-align: center; font-style:bold; font-size: 1.3em;’> You must be logged in to view a member profile</div>”; ?>
    <?php endif; ?>
    

    Change the div inline style to whatever you need accordingly to your theme. The example fits with bp-default.

    If you can not do that, then try this plugin,
    plugin

    Try this but make sure to change the url to what you want

    add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
    
    function redirect_non_logged_users_to_specific_page() {
    
    if ( !is_user_logged_in() && is_page('add page slug or i.d here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
    
    wp_redirect( 'http://www.example.dev/page/' ); 
        exit;
    }
    
  2. Try this in your theme/functions.php or in bp-custom.php:

    function lukedi_private_check() {
    
        if ( ! is_admin() && ! is_user_logged_in() ) {
    
            if ( is_front_page() || is_home() || bp_is_register_page() || bp_is_activation_page() )
                return;
    
            $redirect_url = trailingslashit( site_url() );  // change this to whatever you need
    
            // member page
            if ( bp_is_user() )  
                bp_core_redirect( $redirect_url );
    
            // bbPress
            if( is_bbpress() ) 
                bp_core_redirect( $redirect_url );
    
            // members loop
            $bp_current_component = bp_current_component();
    
            if ( false != $bp_current_component ) {
    
                if ( 'members' == $bp_current_component )
                    bp_core_redirect( $redirect_url );
    
            }
    
        }
    }
    add_action( 'bp_ready', 'lukedi_private_check' );
    

Comments are closed.