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. Please explain it to me… Can I see an example?  I'm using a different template now so this doesn't work.

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 != '') { ?]

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".

[?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 »'); ?] [?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. Here is an example of it in action: Protected Page. I'm using a different template now so this doesn't work.

One Response to “User only/protected pages in WordPress”

  1. ming Says:

    You can read more about this code here: http://www.redwingstudio.com/2007/04/05/user-onlyprotected-pages-in-wordpress-2/

Leave a Reply