• Resolved EyePhoto

    (@eyephoto)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I am trying to add this code to my 2.7 installation. The first piece works perfectly, but I cannot get page.php to redirect to my login or registration page or return the “Need to be logged in” message. I would like a guest (anyone not logged in) to be taken directly to my registration page if they attempt to access a “hidden” page directly by url.

    Am I missing something (and keep in mind I am a coding idiot ?? and new to relatively WP.

    Thanks,

    David

    Michael,
    Thanks for the quick response!

    I believe that is the snippet that Eyephoto was using in his example above, but I can’t actually get it to redirect to the login page. I’m sure its syntax, but here is what I’m trying to insert (fro Eyephoto’s example above):

    <?php if ( is_page('45') && !$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 endif; ?>

    If I try to access the hidden page directly by the url, what I’m getting is the “hidden” page itself (the one I’m trying to redirect away from if the user is not logged in).

    should my redirect be something other than "<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>"

    The “Member Access” plugin does this.

    Been looking for that one forever! Thanks PiggyGirl! That’s the ticket!

    Nice plugin btw, it seems to do what Im looking for although I’ve been having a problem with the plugin.

    Im simply trying to make a private page only accessible to members by checking the following Page settings to:
    ‘By default, only members can view pages’
    and
    ‘Perform redirect for non-members’
    (and choose ‘go to the wordpress login’)

    but when I do so and save the changes it and i try to access the url directly
    (https://www.accedian.com/blog/live-event-registration/)it does not work redirect
    if im not a member. It displays the 404 error page.

    I made sure to ‘Honor the default page visibility settings’ in the page
    itself.

    Im using wordpress 2.7.1

    and you can test the private page link here:
    https://www.accedian.com/blog/live-event-registration/

    Thanks.
    Patrick

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show certain pages to registered users only.’ is closed to new replies.