Skip to:
Content
Pages
Categories
Search
Top
Bottom

Private Portal with BuddyPress

  • Avatar of Christine
    Christine
    Participant

    @mossyoak

    Hi there

    I am working on a personal project making a private client portal with BuddyPress.
    I’ve made a child theme for Twenty Thirteen that does this- thanks to the new update to BuddyPress that lets any theme use buddypress out of the box.
    It uses page templates to assign whether pages are private or public. If they are private it directs non logged in users to the login page.
    I have multiple headers for different levels of privacy that are assigned to different page templates.

    I just wondered if anyone could help me solve a problem.

    I wanted to make it so that the members page and general activity page’s could be made so that only an admin or editor could view them but page templates don’t seem to work with these pages.

    I’ve used conditionals for certain areas of BuddyPress to force certain headers: e.g.

    elseif (bp_is_user_profile() || bp_is_user_activity() || bp_is_user_groups() || bp_is_user_blogs() || bp_is_user_friends() || bp_is_user_messages() || bp_is_user_forums() ):
    get_header('restrict-profile');

    I think that would work with the activity and members page but I’m not sure what the conditionals are for those.

    Please could someone help?
    Thanks

Viewing 15 replies - 1 through 15 (of 15 total)
  • Avatar of Henry
    Henry
    Participant

    @henrywright-1

    You could use the WordPress conditional is_page() to check if the user is on the activity or members page:

    if ( is_page('members') || is_page('activity') ) {
    
    } else {
    
    }

    Note: I’m assuming you haven’t changed the default slugs

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    Thanks for your help Henry- I tried the is_page conditional but it does not work with the BuddyPress Pages.

    So I tried bp_is_Page which worked, but was depecated.
    Then I tried
    bp_is_current_component( 'activity' ) || bp_is_current_component( 'members' )
    and they work for what I want.

    Thanks for your feedback it helped a lot :)

    Chris

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    There is still a problem though in that if I do this it stops any member who is not an admin level user accessing their profile page. Not sure why it should affect that- tried with just the activity conditional and it stops profile page access as well.

    elseif (bp_is_current_component( 'members' ) || bp_is_current_component( 'activity' )) :
    get_header('restrict');

    I’m using a header-restrict.php which redirects them with this redirect at the very top of it:

    <?php if(!current_user_can('delete_others_pages')) { wp_redirect( home_url() ); exit; } ?>

    Any ideas?

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Try this

    <?php
    
       $user_id = bp_loggedin_user_id(); 
       $user = new WP_User( $user_id );
       
       if ( $user->roles[0] != 'administrator' ) {
         // your code
       }
    
    ?>
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Just read through your original question. The code snippet you’d want to add to your theme’s functions.php is something like this:

    function bp_my_restrict() {
       global $bp;
       $user_id = bp_loggedin_user_id(); 
       $user = new WP_User( $user_id );
    	
          if ( bp_is_current_component('members') || bp_is_current_component('activity') ) {
    
             if ( ( $user->roles[0] != 'administrator' ) ) {
                wp_redirect( $bp->loggedin_user->domain );
    	    exit();
    	 }
          }
    }
    add_action('wp','bp_my_restrict');
    Avatar of Christine
    Christine
    Participant

    @mossyoak

    Hi
    Thanks for your help again
    I tried your function

    function bp_my_restrict() {
       global $bp;
       $user_id = bp_loggedin_user_id(); 
       $user = new WP_User( $user_id );
    	
          if ( bp_is_current_component('members') || bp_is_current_component('activity') ) {
    
             if ( ( $user->roles[0] != 'administrator' ) ) {
                wp_redirect( $bp->loggedin_user->domain );
    	    exit();
    	 }
          }
    }
    add_action('wp','bp_my_restrict');

    But it gave a redirect loop error- I think it is because these conditionals are stopping the profile page being accessed for users lower than admin so trying to redirect to it is just looping.

    I changed the redirect to the home page which stopped the constant loop- but users who are not admin can still not access the profile page- these conditionals are stopping that which is a bit odd.

    // BuddyPress restrict activity and members page
    function bp_my_restrict() {
    global $bp;
    $user_id = bp_loggedin_user_id();
    $user = new WP_User( $user_id );
    
    if ( bp_is_current_component('members') || bp_is_current_component('activity') ) {
    if ( ( $user->roles[0] != 'administrator' ) ) {
    wp_redirect( home_url() ); exit;
    }
    }
    }
    add_action('wp','bp_my_restrict');

    Thank you for your help.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Hi @mossyoak

    I’ve run a few tests and made some slight changes to that code. Try this

    function bp_my_restrict() {
       $user_id = bp_loggedin_user_id(); 
       $user = new WP_User( $user_id );
    	
          
    
             if ( ( $user->roles[0] != 'administrator' ) ) {
    		 
    		 if ( bp_is_current_component('members') || bp_is_current_component('activity')  ) {
                wp_redirect( home_url() );
    	    exit();
    	 }
          }
    }
    add_action('wp','bp_my_restrict');
    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    Perhaps a simpler way, given that she wants to check for both admin and editor, is to test against a cap that both admin and editor have.

    Something like: `function bp_my_restrict() {

    if ( !current_user_can(‘edit_posts’) ) {

    if ( bp_is_current_component(‘members’) || bp_is_current_component(‘activity’) ) {

    bp_core_redirect( home_url() );

    }
    }
    }
    add_action(‘wp’,’bp_my_restrict’);`

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    Hi @shanebp and @henrywright-1

    Thanks for your help- I agree with Shanebp that his function does the same thing and is simpler, however I still have the same problem in that it stops access to the members PROFILE page.

    I think this must be a problem with BuddyPress though- maybe it’s a bug?

    Anyway thank you both for helping with the functions I think it will be handy for other things until I sort that problem out.

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    So you want to block
    yoursite.com/members/
    and
    yoursite.com/activity/
    from users who aren’t admins or editors ?

    But allow everyone to view
    yoursite.com/members/my-profile?

    Then you might try putting a current_user_can conditional directly in the over-ride templates for those two pages.

    [there surely is a way to write a function to do this, but it escapes me at the moment]

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    Thanks for the feedback

    I think I understand now- the profile page has activity on it so it’s not possible to do what that way, and it’s linked to the member page because that is showing profiles.

    I could hide things from members with template overrides as you suggest- but I’m not sure if it will work with my theme being a child theme of Twenty Thirteen. If I put the BuddyPress override template files in my child theme will it work?

    Thanks

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    I just read the new Codex on child themes- so it should work with any theme- (answered my own question)

    Thank you for putting up with another newbie guys :-)

    Best

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    bp_is_profile is used to test if its a profile page. try that in a conditional.

    Avatar of Christine
    Christine
    Participant

    @mossyoak

    Hi

    Thanks @modemlooper I managed to figure it out after trying a few things and your conditional helped me deduce it.

    if ( bp_is_current_component(‘activity’) && !bp_is_user_activity() ) {

    worked for me.
    (redirect if on main activity page but not own activity)

    Avatar of Hugo
    Hugo
    Moderator

    @hnla

    Just to add a footnote to thread:

    if by ‘main activity page’ we mean the /activity/ A Directory then we should be able to test for that with:

    bp_is_directory()

    Then not have to qualify the conditional with ! user_activity or at least be able to do bp_is_directory && whatever comp we’re after.

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

You must be logged in to reply to this topic.