Snippet to make a BuddyPress social network private and force registration

Here is a little piece of code that is very useful to me in my BuddyPress sites. It allows you to make the social network private, and to access it, you must become a member.

 

[code]function sqr_prevent_search_guest_user() {
if ( class_exists( 'BuddyPress' ) ) {
if (!is_user_logged_in() && (bp_is_directory() || bp_is_user())) {
wp_redirect(home_url() . '/register/');
exit;
}
}
}
add_action('template_redirect', 'sqr_prevent_search_guest_user');[/code]

This code therefore forces the visitor, if he is NOT connected to your site – if (!is_user_logged_in() – AND he launches the network members page (directory) OR the page of a member (bp_is_user), this redirects him to the registration page, here /register/ in this case. You can change /register/ to your own registration page.

Don't miss business advice by email

No spam, just an email when new content is published.

And what's more, this code is WPML compatible!

If you don't want to touch the functions.php file to incorporate this piece of code, use the very practical My Custom Functions, which will allow you to easily inject your codes from the WordPress dashboard.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

🥦 Potassium, essential for alkalizing the body. Find it in the
✔️ Legumes
✔️ Almonds
✔️ Spinach, broccoli, parsley
✔️ Lawyers
✔️ Oily fish
✔️ Artichokes, mushrooms
The ultimate guide to #Potassium in #keto can be found at https://buff.ly/4aAbFhX

🥩 Offal, forgotten nutritional treasures.
Rich in vitamins and minerals, organ meats like liver are excellent sources of protein and essential nutrients.
👉 Find the complete guide to the ideal diet on http://blooness.com.

Test, review and price of terminals for restaurants: here is my updated overview in 2025 of existing terminal solutions, with their advantages and weaknesses. https://buff.ly/3WAULtG

Load more

Don't miss business advice by email

No spam, just an email when new content is published.

en_USEN