Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to control who can see a page in BuddyPress and restrict anyone else from accessing it.

Something along the lines of the following pseudocode:

<?php if ( loggedin_user_ids==1,4,5,7 ) { ?>
// Show page
<?php } else { ?>
// "You are not allowed to view this page" message
<?php } ?>

So basically, I need:

  1. To be able to control who can see the page, not just any logged in user.

  2. I also need to control which pages this applies to, not just any page.

I am new to BuddyPress so I don't know which functions and variables to use here.

I imagine something like this could be done in the template files, or maybe in the bp-custom.php file. Or is there a better solution to this?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I am new to BuddyPress so I don't know which functions and variables to use here.

Read the codex - for example: http://codex.buddypress.org/developer-docs/the-bp-global/

You could create a function in your theme- functions.php or in bp-custom.php and call it from template files and pass it parameters like allowed_users, etc.

Or you code hard-code something like this into specific template files.

 global $bp; 
 $allowed_users_array = (1,4,5,7);
 if (in_array($bp->loggedin_user->id, $allowed_users_array)) { 
// Show page
} else { 
// "You are not allowed to view this page" message
// or load a custom error page using locate_template( array( 'some.php' ), true );
// or redirect somewhere by using bp_core_redirect( );
} 
share|improve this answer

Have actually been doing some ACL research recently and came across Role Scoper which has seemed to fit my needs quite nicely for the project. I'm providing that link with an additional note that these "all-in-one" solutions can be a bit dodgy, so make sure you test thoroughly. Since you're looking for BuddyPress integration, they also have a sister product, called Press Permit. Haven't tried it, but does appear to do what you need (if you don't want to continue to fill in userIDs until you go blue) :)

share|improve this answer

I did something like this a while ago. I think I've put a template file 'bbpress.php' in my theme folder. In the header I put the following code for access control:

if ( !is_user_logged_in() ) {
    wp_die('You don’t have access to this page.');
}
share|improve this answer
    
Thanks, that's a start. However I need to control who can see the page, not just any logged in user. And I also need to control which pages this applies to, not just any page. (So I guess for that last one, I'd have to move this code to the appropriate template page.) –  Paul T. Aug 17 '12 at 12:49
1  
Yes, you can make different templates to put the code in for different pages. You will have to add an extra check to the if statement to give certain users or roles access. –  Robert Bouten Aug 17 '12 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.