Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Restricting particular pages to particular users (not just roles) WordPress

  • SOLVED

Hi

I need to be able to restrict certain menu items and pages to particular <strong>users</strong> only.

Please note: I am not talking about limiting access to particular <strong>roles</strong>, but individual <strong>users</strong>.

Example: I have 2 users, Alan and Bob, who are both customers of mine. When Alan logs in, a menu item called <strong>Alan's Pages</strong> appears. He can click on this menu item, and see a page or a list of pages relating to his account. Typically, these might contain invoices and quotes exclusive to him, and commercially confidential.

Meanwhile, Bob logs in. Bob now sees a menu item called <strong>Bob's Pages</strong> etc, but he doesn't see <strong>Alan's Pages</strong>, and doesn't even know that Alan exists. Likewise, Alan will not see Bob's Pages, nor know that he exists.

Can someone help please? I'm pretty certain that I can't do this out of the box, but am presuming that a combination of custom fields (or possibly just categories) and a couple of lines of code will do the trick. Alternatively, if there is a plugin (premium or free) that will achieve this, I'd love to know.

Thanks.

Answers (6)

2010-09-01

Pippin Williamson answers:

You could do something like this:


if ( is_user_logged_in() && is_author('Alan') )
{
show content here
} else {
show error message
}

2010-09-01

Monster Coder answers:

hello,
I did not hear any plugin for this purpose! But you can hire a wordpress developer to develop a plugin for you!

If you want to show specific page to specific user, it can be simple. Just ask your developer to make a plugin where you can allow access of specifc user to specific page! like:
PAGE X is visible to user x, user y, user z
PAGE Y is visable to user x, user z

etc. !

It would be quite simple!

But if you need stuffs like invoicing or custom things (rather than post/page), it gonna be quite medium to big sized plugins!

cheers!


Andy Lynam comments:

No, the invoices etc are just examples of the sort of content that might be contained there, to give an indication of why this content would be private. They are not part of this problem.


Monster Coder comments:

ok, then if pages/posts are the concerned, a plugin with admin page to define the accesses will be best for you! There are ways in wordpress to restrict contents to user!

2010-09-01

Eddie Moya answers:

You might be able to use custom fields to restrict the pages.

Each of the pages your describing would have a custom field set to something like
user_permitted = <username>
So, in the page template itself, you could use get_post_meta to check if the <username> is the name of the person logged in.

On the other hand, you could leverage the wordpress a different way. You could have the user set as the author of that page - then simply have the content of that page generated based off data associated with that user. As user meta, or in the options table. Several possibilities lend themselves.

I assume you already have most of what you described - since I cant imagine your asking for someone two explain or create all that for $5. As such, if you explain a bit more of how its set up, I might be able to get more specific as to what to do.


EDIT
----
Scratch the bit about making the user an author of the page.. that would be unnecessary. In general, you just need to associate the content, with a username or user id. From there, making a page that displays that information based on the current user is fairly simple.

2010-09-01

Maor Barazany answers:

I assume something like that might be achieved adding custom post type and add a fileld with the user name (you may even use the built in author filed for example).
Than in the code that renders the UI of that content type you may determine which user is logged in and show posts from the type that matches the username that is logged in with the field that assigns the post to the user.

Than the user Bob will see only his custom posts and user Allan will see only his.

2010-09-01

Khanh Cao answers:

In your page template (default page.php), you can add a list of user names as custom fields and check the current user against them.

To get the list of users (as custom fields), use get_post_meta()

[[LINK href="http://codex.wordpress.org/Custom_Fields"]]WP Custom Fields Codex[[/LINK]]

To get the current user use:


$current_user = wp_get_current_user();
$id = $current_user->ID;
$user_name = $current_user->user_login;


For example:

$current_user = wp_get_current_user();
$users = get_post_meta(get_the_ID(), 'users', false);
$accepted = false;
if (is_array($users))
{
foreach ($users as $user)
{
if ($current_user->user_login == $user)
{
$accepted = true;
}
}
}

if (!$accepted)
{
// stop showing the page
}

2010-09-01

Roberto Mas answers:

Check out this plugin it could serve your needs http://wordpress.org/extend/plugins/role-scoper/