Show certain pages to registered users only.
-
Just thought I’d share what I’m trying to do at the moment on a local install of WP 2.5 and as I’ve seen similar requests, so may lead to a quick and dirty solution.
Q: How do I show certain pages only to logged in users ?
A: The first step I’ve taken is to include this in the header of the theme where it shows the pages:
<ul> <?php if ( $user_ID ) : ?> <?php wp_list_pages('depth=1&title_li=&exclude=' ); ?> <?php else : ?> <?php wp_list_pages('depth=1&title_li=&exclude=5' ); ?> <?php endif; ?> </ul>
This shows all the pages to logged in users but does not show ‘Page 5’ to guests just visiting.
(Issue) As it wasn’t complete you could still browse to the page if you knew the real URL.
The next part I suppose was to include code into the ‘page.php’ to redirect/inform guests that they ‘Need to be logged in to view the page’ and I was using/adapting code from ‘comments.php’:
<?php if ( is_page('5') && !$user_ID ) : ?> <p> You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to view this page. </p> <?php else : ?>
[Changed to suit]
and put the:
<?php endif; ?>
just before the last </div> of the ‘page.php’
(RE: Issue) Now if you do know the real URL it now reports that “You must be logged in to view this page.” and once logged in it redirects you to the page and the page shows up in the menu.
Please note I’m not a coder just trying to implement features for my own site and the code may not be ‘clean’ so any pointers are appreciated.
I’ve taken this method as opposed to trying to get ‘another’ plug-in to work as I’m trying to keep the plug-ins down to a minimum.
- The topic ‘Show certain pages to registered users only.’ is closed to new replies.