Support » Plugins » How to make your blog’s content appear only to lo

  • Say you have a site that you use with friends to share information and discuss things, but you don’t want it open to the public. Instead, you want alternate content – say some Google ads – to appear on the site to people who aren’t logged in. You don’t want to password-protect the entire site; you want it to look normal to people who aren’t logged in, so they don’t even know what they’re missing.
    Here’s how to do it, via some code adapted from
    First, find “the loop,” and cut and paste it into a new file on your computer called “private.php” The loop is this section of code:
    <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    <?php the_date('','<h2>','</h2>'); ?>
    <div class="post">
    <h3 class="storytitle" id="post-<?php the_ID(); ?>">" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></h3>
    <div class="meta"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></div>
    <div class="storycontent">
    <?php the_content(); ?>
    <div class="feedback">
    <?php wp_link_pages(); ?>
    <?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
    <?php trackback_rdf(); ?>
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endforeach; else: ?>
    <?php _e('Sorry, no posts matched your criteria.'); ?>
    <?php endif; ?>

    Got it? Now, where The Loop used to be, insert this code instead (note: change the userlevel to whatever you want, 1-9):
    global $HTTP_SERVER_VARS;
    global $user_level;
    if ($user_level < 1) {
    else {

    Now we need to create a file of code that the public (not logged in) sees. Put whatever you want in this file (remember, it’s not a standalone page, so it doesn’t need any thing special like the html and head tags). The point of this whole thing (as opposed to just password-protecting your entire site) is that there’s still something good visible for people who aren’t logged in. Save this file as public.php. Upload public.php and private.php to your blog’s root directory, the same folder where index.php is.
    Check it out. Make sure the page still loads when you’re logged in, then logout and go back to the blog to see if the content has changed as intended. Good.
    One last thing: We need to delete the RSS feed-generating PHP files, or else it won’t be very private. In the current versions, the RSS/Atom feed files are wp-atom.php, wp-rss.php, wp-rss2.php, and wp-commentsrss2.php. Whew!
    Finally, go into your index.php template and remove the links to the now-gone RSS feeds, and modify the login settings as desired. For example, you may want to enable people to register, but not post until you raise their level, or you may not want people to be able to register.
    Have fun. Let me know if there are any issues. Email me at justin at baederresources dot com.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The problem with this is that the login takes the user to the post interface.
    After logging in the user should ideally get to the main viewable weblog and then have an option to go to the post interface.

    I don’t really know PHP; could you give an example of what it would look like with the updated code?
    That can be fixed by using the following as the login link:
    You don’t get the fancy-dancy text that changes from login to logout depending on your login status, but it works, and it saves clicks.

    Thanks justin, that works just fine now…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to make your blog’s content appear only to lo’ is closed to new replies.