Make Pages Accessible Only to Registered Members
Posted by Asuka_Aki | Posted in Uncategorized | Posted on 16-09-2008 |
The Plan
I recently began working on some WordPress themes. I wanted a themne viewer so I created a page that could serve that purpose. I also wanted that page to be accessible only to members.
The Solution
So what I did was download a copy of Links (links.php) via FTP to have a valid page template. If your theme doesn’t have one simpley make one with note pad or some editor. A page template is simply a php document that begins like so:
1 2 3 4 | <?php /* template name: members (what ever name you want to give it shows up in the admin’s template editor) */ |
After that come the get header, conteent, sidebar and footer…You can use your index as a guide.
The Code
For a complete example, this is my member’s page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /* template name: members */ ?> <?php get_header(); ?> <?php if (is_user_logged_in()) { ?> Oranges and lemons <? } else { ?> You must be logged in! <? } ?> <?php get_footer(); ?> |
Explanations
Where you see “Oranges and lemons”, this is the place you add the code for the content you want to be visible only to registered and logged in users.
“You must be logged in!” Can be changed to what ever content you want to be visible for every one.
Want To Get Fancy
Where it says, “you must be logged in you can add a login / register form by adding the following code that is from the wp-admin login and register pages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <form name="loginform" id="loginform" action="http://yoursite.com/wp-login.php" method="post"> <p> <label>Username<br> <input name="log" id="user_login" value="" size="20" tabindex="10" type="text"> </label> </p> <p> <label>Password<br> <input name="pwd" id="user_pass" value="" size="20" tabindex="20" type="password"> </label> </p> <p> <label> <input name="rememberme" id="rememberme" value="forever" tabindex="90" type="checkbox"> Remember Me</label> </p> <p> <input name="wp-submit" id="wp-submit" value="Log In" tabindex="100" type="submit"> <input name="redirect_to" value="yoursite.com/wp-admin" type="hidden"> <input name="testcookie" value="1" type="hidden"> </p> </form> <p id="nav"> <a href="yoursite.com/wp-login.php?action=register">Register</a> | <a href="yoursite.com/wp-login.php?action=lostpassword" title="Password Lost and Found">Lost your password?</a> </p> |
So hope that helps and for a live example look at the top of the page and click “Free Themes” it is an example of this blog post.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!



