wordpressbuddypress

How to force user for login if they are searching some profiles with BuddyPress


I want to make a profile directory with BuddyPress, but I want Website should asks for user login/register on search of any profile form the website. In describe to that if someone wanted to search any profile as per gender, location or skills, the user should be login to view all the searched profiles. I have tried below code but it is not working.

function bp_guest_redirect() {
        global $bp;
        if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() ||     bp_is_page( BP_MEMBERS_SLUG ) ) {
        // enter the slug or component conditional here
                if(!is_user_logged_in()) { // not logged in user
                        wp_redirect( home_url("/register/" );
                } // user will be redirect to any link to want
        }
}
add_filter('get_header','bp_guest_redirect',1);

Please suggest how I should redirect a user to login/register page after any search.


Solution

  • This code only restricts access to the members directory search feature and uses the bp_before_directory_members_content action hook to check if the user is not logged in and redirect them to the login page using the wp_login_url function.

    function bp_guest_redirect() {
        if ( bp_is_members_component() && ! is_user_logged_in() ) {
            wp_redirect( wp_login_url() );
            exit();
        }
    }
    add_action( 'bp_before_directory_members_content', 'bp_guest_redirect' );