/ Published in: PHP
                    
                                        
You can create a page template that you can apply to pages so only logged in members can view the contents:
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<?php
/*
Template Name: Members only page
*/
?>
<?php get_header(); ?>
<?php if (is_user_logged_in()) { ?>
<div id="contents">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 class="title" id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
<?php the_content(__('Read the rest of this page »')); ?>
<?php endwhile; endif; ?>
</div>
<?php comments_template(); ?>
<?php } else {
// they aren't logged in, so show them the login form
?>
<div id="contents">
<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>
</div>
<?php
// !!! IMPORTANT !!! If you are using permalinks to rewrite the urls
// of your site, you will need to adjust the action of the form below.
// The form will look for the wp-login.php file based on the action.
// If the rewritten url of your page is
// http://www.yourdomain.com/your_page_name/,
// you will need to change the action in the form to
// action='../wp-login.php'
// so it will look in the 'directory' above /your_page_name/
// for the wp-login file.
// Depending on your url structure, you may have to go up several directories.
// For instance, if your page is located at
// http://www.yourdomain.com/some_section/your_page_name/
// you will need to change the action to
// action='../../wp-login.php'
?>
<form name='loginform' id='loginform' action='<?php bloginfo('wpurl'); ?>/wp-login.php' method='post'>
<p>
<label>Username<br />
<input type='text' name='log' id='log' value='' size='20' tabindex='1' />
</label>
</p>
<p>
<label>Password<br />
<input type='password' name='pwd' id='pwd' value='' size='20' tabindex='2' />
</label>
</p>
<p>
<label>
<input name='rememberme' type='checkbox' id='rememberme' value='forever' tabindex='3' />
Remember Me</label>
</p>
<p class='submit'>
<input type='submit' name='submit' id='submit' value='Login »' tabindex='4' />
<?php //use a hidden field to return them to the page they came from ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER["REQUEST_URI"]; ?>" />
</p>
</form>
<?php } ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Comments
 Subscribe to comments
                    Subscribe to comments
                
                