You can create a page template that you can apply to pages so only logged in members can view the contents:


/ Published in: PHP
Save to your folder(s)

You can create a page template that you can apply to pages so only logged in members can view the contents:


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Template Name: Members only page
  4. */
  5. ?>
  6.  
  7. <?php get_header(); ?>
  8. <?php if (is_user_logged_in()) { ?>
  9. <div id="contents">
  10. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  11. <h2 class="title" id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
  12. <?php the_content(__('Read the rest of this page &raquo;')); ?>
  13. <?php endwhile; endif; ?>
  14. </div>
  15. <?php comments_template(); ?>
  16.  
  17. <?php } else {
  18. // they aren't logged in, so show them the login form
  19. ?>
  20. <div id="contents">
  21. <p>I'm sorry, but you must be a logged in member to view this page. Please either login below or <a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=register">Register</a> today.p>
  22. </div>
  23. <?php
  24. // !!! IMPORTANT !!! If you are using permalinks to rewrite the urls
  25. // of your site, you will need to adjust the action of the form below.
  26. // The form will look for the wp-login.php file based on the action.
  27. // If the rewritten url of your page is
  28. // http://www.yourdomain.com/your_page_name/,
  29. // you will need to change the action in the form to
  30. // action='../wp-login.php'
  31. // so it will look in the 'directory' above /your_page_name/
  32. // for the wp-login file.
  33. // Depending on your url structure, you may have to go up several directories.
  34. // For instance, if your page is located at
  35. // http://www.yourdomain.com/some_section/your_page_name/
  36. // you will need to change the action to
  37. // action='../../wp-login.php'
  38. ?>
  39. <form name='loginform' id='loginform' action='<?php bloginfo('wpurl'); ?>/wp-login.php' method='post'>
  40. <p>
  41. <label>Username<br />
  42. <input type='text' name='log' id='log' value='' size='20' tabindex='1' />
  43. </label>
  44. </p>
  45. <p>
  46. <label>Password<br />
  47. <input type='password' name='pwd' id='pwd' value='' size='20' tabindex='2' />
  48. </label>
  49. </p>
  50. <p>
  51. <label>
  52. <input name='rememberme' type='checkbox' id='rememberme' value='forever' tabindex='3' />
  53. Remember Me</label>
  54. </p>
  55. <p class='submit'>
  56. <input type='submit' name='submit' id='submit' value='Login &raquo;' tabindex='4' />
  57. <?php //use a hidden field to return them to the page they came from ?>
  58. <input type="hidden" name="redirect_to" value="<?php echo $_SERVER["REQUEST_URI"]; ?>" />
  59. </p>
  60. </form>
  61. <?php } ?>
  62.  
  63. <?php get_sidebar(); ?>
  64. <?php get_footer(); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.