Securing ‘members only’ pages in WordPress

This is an easy way to secure individual pages on your WordPress website to only give access to those with an account on your WordPress site.

Create a php file ‘page_secure.php‘ in your theme folder and paste in the following code.

Then when ever you add or edit a page, you can choose the ‘Members Only’ template from the Template drop down under the page attributes panel (shown right).

 * Template Name: Members Only
<?php get_header(); ?>

<?php if (is_user_logged_in()) : the_post(); ?>
  <h1><?php the_title(); ?></h1>            
  <?php the_content(); ?>    
<?php else : ?>
  <h1>Members only: <?php the_title(); ?></h1>
  <p>Please login with your account information</p>
  <?php wp_login_form(); ?>
  <p><a href="<?php echo wp_lostpassword_url(); ?>" title="Lost Password">Lost Password</a>
| <?php echo wp_register('', '', false); ?></p>
<?php endif; ?>

<?php get_footer(); ?>