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.
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
Nice tip. Thank you Jean!
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?
@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:)
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!
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()?
Sure, it is possible to create a function to do that. I should think about it for a recipe
Sounds great – I’ll look forward to seeing it. If I do it first, I’ll let you know :0)
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?
thanks a lot… i really like this tips
Thanks for sharing ! very usefull post !
Some good point. Thank you for the tip.
I’m also very keen for this private page per user recipe!
Thanks for sharing ! very usefull post !
nice code, thanks !
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: