User only/protected pages in WordPress

Here is some fairly simple code for restricting pages only to logged in users in WordPress. Just give me the code |

First, you have to set up a template for the page. You create templates for pages by adding some code at the top of the page, like so:

< ? php /* Template Name: NameOfTemplatePage */ ?>
Then toss in the rest of your template and upload it to your site. It should look something like this: template example. Next, you have to assign the template to the page that you want. Edit the page you want to assign the template to. On the right you’ll see a drop down called “Page Template”. It will not show up onscreen until you have created at least one page template and put it in your template directory. In this case, we would choose the template called “NameOfTemplatePage”. Okay, now comes the fun part, we need to restrict access to the content on this page to users who are logged in. First, we need to find out if they are logged in. We can do that fairly simply:
< ? php
// is the user logged in?
global $userdata;
get_currentuserinfo();
//assign it a variable
$validuser = $userdata->ID;
//if the user is logged in, the $validuser variable will have an ID assigned to it, so check to see if it is empty
if ($validuser != '') { ?>

[UPDATE] As Dan MacTough pointed out below, the above code could be replaced with:
< ? php if (is_user_logged_in()) { ?>
I wasn’t aware of that function, so I wrote it using a php variable instead. Either way works so take your pick. (Thanks Dan!)[/UPDATE]

If the user is logged in (i.e. if $validuser has a value and isn’t blank), then show them the logged in user content. We want to choose the name of the content to show them based on the post slug of the page, in this case, “nameoftemplatepage”. You can do this code below to choose the post you want, but only if it is a mainlevel page, it will not work if the page is a sub-page of another page. If you do not use the code below, you can use the template for multiple pages in the event you wanted to protect multiple pages (on the same level).
< ? php
// show them the nameoftemplatepage page
// grab the appropriate page that we want "nameoftemplatepage"
query_posts('pagename=nameoftemplatepage');
?>

Then you toss in the normal loop for grabbing content. I’ve stripped the divs and header tags out of here so it shows up as code.
< ? php if (have_posts()) : ?>
< ? php while (have_posts()) : the_post(); ?>
< ? php the_title(); ?>
< ? php the_content('Read the rest of this entry &raquo;'); ?>
< ? php edit_post_link('Edit', '', ''); ?>
< ? php endwhile; ?>
< ? php else : ?>
Not Found Sorry, but you are looking for something that isn't here. < ? php include (TEMPLATEPATH . "/searchform.php"); ?>
< ? php endif; ?>

Then we need to give the alternate content to the user, in my case, I used a login form.
< ? php } else {
// they aren't logged in, so show them the login form
?>
You need to log in to access this content.
Login form goes here
< ? php } ?>

Since WordPress keeps trying to process the form code even though it’s within code tags, you can click on this link to see the full document in php: protected page example.

59 Responses to “User only/protected pages in WordPress”

  1. Rupert says:

    Ben,
    I was wondering if I could take Ralphie’s concept a bit further. What if I wanted to assign different folks registering at the site to different group with different access to different pages.

    For ex. Group 1 (role 1) can access protected pages 1 and 3
    Group 2 (role 2) can access protected pages 2 and 4.

    How could I achieve that? thanks.

  2. Ben says:

    Rupert,

    It should be fairly simple to do what you want. As in Ralphie’s case, you’ll need to create the capabilities using the Role Manager plugin. For what you want to do, you’ll need two capabilities. You can name them whatever you want: “View My Hidden Posts” and “View My Other Hidden Posts” might be options. To do that go to Users –> Capabilities and type “View My Hidden Posts” minus the quotes into the New Capability Name input box and click Create Capability. Now do the same for “View My Other Hidden Posts”.

    Create two new roles or use existing roles and then just check off the new capability that is applicable for each role (either View My Hidden Posts or View My Other Hidden Posts). For this example, let’s call those we want to assign to Group 1 “Studs” and those in Group 2 “Duds”.

    Then, you have a choice, you can incorporate this all into one page template or have two different page templates. The simplest method is probably making two different page templates and assign whichever template is appropriate to the page. Go grab a copy of this file here: http://www.redwingstudio.com/downloads/user-and-role-protected.phps (copy and paste it into your own blank php page and make two copies).

    You can name the templates whatever you want. In WordPress you name page templates by adding this at the top of the page:

    < ?php /* Template Name: Hidden Page
    */ ?>

    Just replace “Hidden Page” on my example template with whatever you want to name your template. For the sake of my example, I’ll call them “Hidden Page for Studs” and “Hidden Page for Duds”.

    Now, you have your two copies of the hidden page template, but you need to make a change to our second one so we show the “duds” only the content that they should see. On the duds template, look for this line:
    if ( current_user_can('view_my_hidden_posts') ) {
    replace it with
    if ( current_user_can('view_my_other_hidden_posts') ) {

    Now, when you create a page that you want to be hidden, just choose the appropriate template from the template drop down. If it’s a page that only studs should see, choose the “Hidden Page for Studs” template. If it’s a page for duds, choose the “Hidden Page for Duds” template.

    I’m not sure how you’re handling your navigation, but if you’re creating links manually or have a separate section of the nav that you’re creating specifically for these pages, you can set it up so that only people who are logged in can see those pages and they would only see pages they had access to be they a dud or a stud. To do that, you’ll just need to wrap a few if statements around your navigation:

    First, wrap this around all the nav links that should be hidden or show depending on whether someone is logged in:


    < ?php
    if (is_user_logged_in()) {
    // your navigation links will go here
    }
    ?>

    Then, you need to add the checks inside of the check for the user being logged in to show content for only the duds and studs:


    < ?php
    if (is_user_logged_in()) {
    if ( current_user_can('view_my_hidden_posts') ) {
    // stud links go here
    } else if ( current_user_can('view_my_other_hidden_posts') ) {
    // dud links go here
    }
    }
    ?>

    Hope the above helps. Let me know if you have any more questions.

    Here are some links to some resources I found helpful when I was working with the capabilities and integrating them into the template:
    http://codex.wordpress.org/Roles_and_Capabilities
    http://boren.nu/archives/2005/12/01/whats-new-in-20-roles-and-capabilities/

  3. Rupert says:

    Ben – it worked like a champ first time. Thank you very much!

    The only issue I ran into, and it has nothing to do with the script, was having it fit seemlessly into my site theme. I was able to poke around and copy some stuff from the single.php and page files in the theme directory and manipulate the stylesheet some, but the result was still pretty amatureish. I am using FTW 2.0 theme by flisterz…one of the best themes that I have seen on the WP platform; if anyone cares to take a stab at melding the protected.phps here with it to create a seemless page template, I would really appreciate it.

    The ironic thing is, this might be a 5 minute job because I know I am pretty close, but not been able to speak programming, my abilities fall short of completing it.

    I would post a link to my site, but I currently only have it on MAMPS on my IMAC.

    Thanks very much again Ben.

    Rupe

  4. [...] is – If you have the password you can view. Here’s a protected page plugin template by redwing studio; condition is – If you are member of this blog, you can [...]

  5. [...] User only/protected pages in WordPress | Redwing Studio – [...]

  6. Mukesh says:

    Hi, I’m wondering if this same method will work for posts. The items here only mention page, but will this work for posts?
    Thanks
    Mukesh

  7. do you have a downloadable plugin for this? because i don’t understand how to apply the codes. thanks!

  8. James says:

    Hi Ben,

    This is exactly what I have been looking for. The only problem is that the download is not available anymore. Could you please send me the file or the link to it?

    Thank you very much,
    James

  9. Ben says:

    @Mukesh,

    Yes, theoretically you could apply this same code to your index.php, single.php, category.php, and archive.php files in your template and force users to log in before viewing the posts.

    @Exam Philippines
    I don’t have a plugin for this but I do think that someone has come out with a plugin sometime in the past year that you can use that works similarly.

Leave a Reply