Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to make section of profile private?


  • screampuff
    Participant

    @screampuff

    Hello, I have Buddypress and Achievements for WordPress (the author is not active or providing support). This is an internal company site and I want to make the Achievements private so that users can only see their own achievements.

    I’ve figured out how to make the xprofile Achievements tab only appear for the current user and admins with this code:

    function hide_achievements_from_users() {
    global $bp;
    if ((bp_loggedin_user_id() == bp_displayed_user_id()) || (current_user_can('manage_options'))){
    return;
    }
    bp_core_remove_nav_item('achievements');
    }
    add_action( 'bp_setup_nav', 'hide_achievements_from_users', 99 );

    However a user can still browse other user’s achievements by manually typing out the url like http://mysite.com/members/username/achievements/

    Is there a way I can make this section of user’s profiles private so that only they (and admins) can view it? Users should still be able to visit another user’s profile, it is only the /achievements/ at the end of the URL which needs to be blocked.

Viewing 14 replies - 1 through 14 (of 14 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @screampuff

    You can make that link private by doing something like this:

    function screampuff_hide_achi() {
        // Bail if the current user is the displayed user.
        if ( bp_loggedin_user_id() == bp_displayed_user_id() )
            return;
    
        // Bail if the current user is an admin.
        if ( current_user_can( 'manage_options' ) )
            return;
    
        // Bail if not the achievements page.
        if ( bp_current_action() != 'achievements' )
            return;
    
        // Redirect.
        wp_redirect( home_url() );
        exit;
    }
    add_action( 'template_redirect', 'screampuff_hide_achi' );

    Please note I haven’t tested. Also, I’m assuming the current action is ‘achievements’. It may be something else in which case the function will need a minor tweak.


    shanebp
    Moderator

    @shanebp

    Minor: You can use bp_is_my_profile() instead of bp_loggedin_user_id() == bp_displayed_user_id()

    And you don’t need the $bp global.


    screampuff
    Participant

    @screampuff

    Thanks for the reply Henry.

    I experimented with the code but couldn’t get it to work. I believe it has something to do with if ( bp_current_action() != 'achievements' )

    I’m not sure if this is the best way to describe it, but when I previously used bp_core_remove_nav_item('achievements'); it made the /members/username/achievements outside of the buddpress page. It basically created a new page that had nothing but a list of the user’s achievements. The part it called from the plugin code. It doesn’t show the username, avatar or anything else from buddypress.

    Also thanks for that update Shane.


    shanebp
    Moderator

    @shanebp

    You could also leave the navigation alone and either hack this file:
    achievements\templates\achievements\achievements\loop-achievements.php

    Or run a user check and redirect on this hook in that file:
    do_action( 'dpa_template_before_achievements_loop_block' );


    Henry Wright
    Moderator

    @henrywright

    @screampuff

    I experimented with the code but couldn’t get it to work

    Looking into it now…

    As an aside, whilst looking through the plugin’s code, I noticed that endpoints (example.com/author/username/achievements) are used if you ever have BuddyPress disabled. Just something to keep in mind.


    Henry Wright
    Moderator

    @henrywright

    Try changing:

    // Bail if not the achievements page.
    if ( bp_current_action() != 'achievements' )
        return;

    to:

    if ( bp_is_user() && bp_is_current_component( 'achievements' ) && bp_is_current_action( 'all' ) )
        return;

    Henry Wright
    Moderator

    @henrywright

    @shanebp completely forgot about bp_is_my_profile() 😀


    screampuff
    Participant

    @screampuff

    No luck with

    if ( bp_is_user() && bp_is_current_component( 'achievements' ) && bp_is_current_action( 'all' ) )
        return;

    My guess is that since I removed the tab with bp_core_remove_nav_item('achievements'); that it is treating it like Buddypress is disabled like you mentioned above.


    Henry Wright
    Moderator

    @henrywright

    You could be right. I think bp_core_remove_nav_item() might do a little more than just visually removing an item from the member’s nav menu.

    If it helps, this is the method that adds the item to the nav menu.


    screampuff
    Participant

    @screampuff

    Thanks for that suggestion Shane, users should still be able to see mysite.com/achievements which simply lists all available achievements. If I put a redirect on that will it prevent users from seeing this?


    shanebp
    Moderator

    @shanebp

    rhetorical questions…
    Did you try it?
    Is the hook I mentioned also in that template?


    screampuff
    Participant

    @screampuff

    Sorry, I should have done it before posting.

    Yes the hook is you mentioned is in that template, I ran the user check and it was successful, however it also removed the main list of achievements.

    Also I was looking here: https://github.com/paulgibbs/achievements/blob/master/src/includes/class-dpa-wpcli-achievements-users-command.php#L33

    And it looks like this is where the list for a specified user is created, but I wasn’t able successfully run a user check on it, but I assume that’s because it’s not going through buddypress.


    screampuff
    Participant

    @screampuff

    Also my site is private and can only be accessed when logged in, so I’m wondering if I can edit line line 47 in that same code to include the user check and simply display the “Invalid User ID specified.” If someone tries to view another user’s achievements list. That would be fine with me.

    I’m still new to WP/BP and PHP in general if you haven’t noticed 😉


    screampuff
    Participant

    @screampuff

    Shane you had it all along, but it’s actually the file content-author-achievement.php that does do_action( 'dpa_template_before_achievements_loop_block' ); for a specific user.

    I put the check user check in that file and it’s working exactly how I wanted.

    Thanks so much for the help guys, I’ve spent too much time trying to do this!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to make section of profile private?’ is closed to new replies.
Skip to toolbar