How To Only Allow Users To View Their Own Buddypress Profiles? [closed]

I am currently developing a social-network sort of site using WordPress + Buddypress and the client has asked that profile pages not be publicly visible for the time being. Basically the client is okay with logged in users viewing their own profiles, but if they try viewing a profile page of another user it should redirect to the homepage.

I found the function: bp_is_my_profile() and tried using the following code at the top of the members/single/profile.php file to redirect users away, but it doesn’t appear to be working. Any pointers?

<?php
if ( !bp_is_my_profile() )
{
    wp_redirect(site_url(), 302);
}
?>

1 Answer
1

I solved this issue myself, it was quite easy and I’m surprised nobody else supplied an answer. Having said that, the solution is to add a few lines of code that check what the author ID is of the profile you’re viewing and compare it to the ID of the currently logged in user.

This code goes at the top of members/single/profile.php

<?php
    // Global $bp variable holds all of our info
    global $bp;

    // The user ID of the currently logged in user
    $current_user_id = (int) trim($bp->loggedin_user->id);

    // The author that we are currently viewing
    $author_id  = (int) trim($bp->displayed_user->id);

    if ($current_user_id !== $author_id)
    {
    // redirect to home page url
        wp_redirect(home_url());
        exit();
    }
?>

Leave a Comment