How to: Restrict page view to authenticated users

WordPress allows you to make a post private, but what about static pages? Here’s a quick, clear and concise recipe to create a private page template.

If you want to make all your blog pages private, you can use the code "as it", but I recommend you to create a page template, so you can decide if the page is public or private when you'll write it in your WordPress dashboard.

If you don't know how to create and use page templates, read this recipe first.

To check if the current reader is authenticated or not, WordPress provide the is_user_logged_in() function, which will return true if user is logged in, false otherwise.

We just have to create a basic php conditional instruction and check if the user is logged in:

if (is_user_logged_in()) {
// You page code goes here
} else {
echo "You must be logged in to view this page.";
}

Insert the code above in a new page template. When you'll want to make a private page, just use this template.

20 Responses

Oct 10 2008 13:00

Nice tip. Thank you Jean!

Oct 20 2008 11:01

Hi, very useful info on the site. I would like to know if this can be extended and use Wordpress’ user system to run a dynamic content site where users can have their own protected page, restricted only to their login?

I would like to let Wordpress manage my user logins and for them to view some content they have uploaded themselves.

This uploaded content would not be available to anyone else.

Is this possible?

Oct 20 2008 11:04

@Britalian: If you want to restrict a page to only one user, yes, this is possible. If you’re interested in this, I should write a recipe:)

Oct 20 2008 11:07

I would be very interested in this!

I wouldn’t be looking to burden anyone to do it for me though, if possible I am looking for some pointers on how to go about it myself.

Having said that, if such a recipe were to turn up on the site, I would be more than delighted!

Nov 05 2008 14:21

Hi, this is interesting, but what I’d really like to know is how to set pages to ‘private’, and have those pages only show up in the navigation if the user is logged in. Is is possible to overwrite the wp_list_pages() function, or copy it with the required alterations, for use in the template, i.e. wp_list_permitted_pages()?

Nov 05 2008 14:37

Sure, it is possible to create a function to do that. I should think about it for a recipe :)

Nov 05 2008 15:14

Sounds great – I’ll look forward to seeing it. If I do it first, I’ll let you know :0)

Feb 22 2009 20:19

Hi, this may be slightly off-topic but why does the cat above say “helped” rather than “help”? Are we dealing with a dyslexic cat here?

Mar 30 2009 19:12

thanks a lot… i really like this tips

Mar 31 2009 17:14

Thanks for sharing ! very usefull post !

Apr 27 2009 14:24

Some good point. Thank you for the tip.

Aug 11 2009 06:15

I’m also very keen for this private page per user recipe!

Oct 02 2009 16:41

Thanks for sharing ! very usefull post !

Oct 06 2009 21:22

nice code, thanks !

Dec 17 2009 03:41

Hi thanks for this.

I’d be very keen for a recipe for one page for a logged in user as well.

Also, do you know how to hide the menu link for the page unless the user is logged in?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required