By default, anyone who comes to your WordPress website can see any page on that website. What if you want your website to be open to everyone, except you really need to have a page or two that only registered, logged in users see? Example? A company intranet, a calendar that’s only for family or a contact info page that’s only for family. Whatever the reason, there are only a few ways to make sure a page isn’t open to the world.
Make the page private
You know what I mean. When you’re editing your page you look over on the right side just above the Publish/Save button an see a “Visibility” option. You can make that private and the page is hidden from the public, but it’s also hidden from all other registered users. When set to private a page also doesn’t show up in the main menus or anywhere else. It’s basically hidden from everyone from the one who made it private.
Password protect the page
Under that same “Visibility” section you can also choose “Password protected”. This is good. The page shows up in the menus and all that, but this option asks for a password for this particular page. Basically, you set a password for this page and then you have to share that password with anyone you want have access to the page. That’s not quite what I’m after.
Create a new page template
So, to recap, I wanted a page that shows up in the menus just like any other page but whose content can only be seen by users who have an account on the website and have logged in. For all others, I want the page content hidden. Here’s what I came up with because I couldn’t find a plugin that accomplished this without annoying upgrade messages or adding a lot of bloat for no reason.
- Create a new page template by making a copy of your theme’s existing page.php template. I named mine “page_logged_in.php”.
- Once you have your page you need to edit the file in whatever editor you like. I use Dreamweaver, but any text editor will work just as well. The first bit of code you need to add names the template so you can find it later. (This is what you’ll select in the WordPress admin when creating your new page.) Just add the following code to the very top of the code. There will most likely be something similar already there. If so, just replace it.Template Name CodePHP12345<?php/*Template Name: Logged-In Users Page*/?>
- Now you need to add a little code to see if the user is logged in. This is pretty easy. WordPress has hooks built in for this. The following can be added directly below the Template Name code above, also at the top of the page before any of the standard WordPress stuff.is_user_logged_inPHP1<?php if(is_user_logged_in()):?>
Now, go to the very bottom of the page and add the following code. This will call wp_die() to halt WordPress and display a notice to the visitor saying they need to log in to view the page.wp_die()PHP12345678<?phpelse:wp_die("<h2 align='center'>To view this page you must first<a href='". wp_login_url(get_permalink()) ."' title='Login'>log in</a></h2>");endif;?>
This little snippet does a couple things. If it runs (if the user isn’t logged in) then it halts the page and displays a message that says “To view this page you must first log in.” The words “log in” are a link created by wp_login_url() which is another WordPress hook. You may notice get_permalink() embedded in wp_login_url(). What this does is gets the address of the page you’re on so that when the user clicks the link to log in, they come back to your password protected page once they log in. You can forward them pretty much anywhere you want, but I like to bring people back to where they started to minimize confusion.
Now just save the page template and upload it to your web server where your template files are. Hopefully you simply duplicated page.php where it already lived, so this is a no brainer.
Next, go to WordPress and create a new page. Under “Page template”, select the new page template we just created.
- Save your new page and that’s all there is to it. That page will now appear just like any other page in your menus, but when a user who is not logged in tries to visit it, they will receive the notice you included above.
Hope this helps. I looked for a way to do this for about a half a day. This seems to be a simply way to go about it.