Support » Requests and Feedback » Privacy based on levels

  • I’d like to be able to protect posts so that users have to sign on to read them, and have their post permissions based on user level, or even more ideally, I’d love to be able to set unix style permissions on posts so that you could assign individual or group priveleges to posts. I’ve seen this alluded to in other posts and have found that most people’s problem is solved by the post levels plugin. This almost works for me, but I want my protected post titles to appear on my main page and have a link that takes you into the post, but asks you to login first. That way people’s feed readers would be updated that I have a new post, but they wouldn’t be able to read the content until they logged in.

Viewing 10 replies - 1 through 10 (of 10 total)
  • skippy



    You could try something like this in your Loop:

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    echo '<div class="entry">';
    if ($user_level > 0) {
    the_content('Read the rest of this entry &raquo;');
    } else {
    echo 'Please login to read this post!';
    } ?>

    1. To hit users of all levels, I’d change skippy’s code above to:

    if ($user_level >= '0') {

    2. The modifications would have to occur in any template of your theme incorporating The Loop.

    3. You’d need to do something similar to your feed scripts (wp-rss.php, wp-rss2.php, wp-rdf.php and wp-atom.php) if don’t want the content to display in your syndication feeds, as well.

    4. If you primarily post long articles, you may want to display a summary of your post content (using the_excerpt()) instead of just an abbreviated “must login” message like above (you could also just set feeds to summary to solve that problem).

    Thanks, that’s basically what I wanted. Now I’d like to set it so that only certain categories are hidden. I’m new to php and am not having much luck doing this, but I was trying something along the lines of
    if (preg_match (“CategoryName”, the_category()))
    {have to login to show post}
    {show full post}

    or using if (strpos(the_category(), ‘CategoryName”))

    but I’m not getting this to work. Any advice?

    WordPress has a conditional “tag” called in_category(), so you could do:

    if ($user_level >= '0' && (in_category(1) || in_category(2))) {

    Just provide the numeric category ID as the argument.

    This has worked better than I anticipated so thanks for the help. There’s still a few issues, but I’m going to post them in a different place since they’re not related to the initial question. Below I’ve pasted in my finished loop that I’m using on my index.php. I haven’t looked at changing the feed loops yet, but I’m sure they’ll be pretty close to the same thing.

    if ($posts) : foreach ($posts as $post) : start_wp();
    <!– If the user isnt logged in and the post is in the personal category, ask to login –>
    if ($user_level <= ‘0’ && (in_category(26))) { ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h2>
    <small><?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –></small>
    <div class=”entry”>
    Please <?php wp_register(”, ”); ?> or <?php wp_loginout(); ?> to read this post.
    <?php }
    else { ?>
    <div class=”date”><?php the_date(); ?></div>

    <h2 id=”post-<?php the_ID(); ?>”>” rel=”bookmark” title=”Permanent Link: <?php the_title(); ?>”><?php the_title(); ?></h2>

    <div class=”meta”><?php _e(“Filed under:”); ?> <?php the_category(‘,’) ?> — <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(); ?></div>

    <div class=”storycontent”>
    <?php the_content(__(‘(more…)’)); ?>

    <div class=”feedback”>
    <?php wp_link_pages(); ?>
    <?php comments_popup_link(__(‘Comments (0)’), __(‘Comments (1)’), __(‘Comments (%)’)); ?>

    <?php trackback_rdf(); ?>

    <?php comments_template();
    } ?>
    <?php endforeach; else: ?>
    <?php _e(‘Sorry, no posts matched your criteria.’); ?>
    <?php endif; ?>

    I tried what skippy said, but my entries end up all messed up, is there something else I can do?




    “messed up” in what way?

    Well the layout is all wrong, and the “please login to read this post” ends up in all my entries, even the ones that don’t have post levels.




    Can you post for us the relevant portion of your template file?

    Sure, and I want to thank you for all ur help as well:

    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

    <div class=”post”>

    <?php the_date(‘m.d.y’, ‘<h1 class=”storydate”>’, ‘</h1>’); ?>
    <h2 id=”post-<?php the_ID(); ?>” class=”storytitle”>” rel=”bookmark” title=”<?php _e(‘Permanent link to’); ?> <?php the_title(); ?>”><?php the_title(); ?></h2>
    <p class=”meta”><?php _e(‘Posted in’); ?> <?php the_category(‘, ‘) ?> <?php _e(‘at’); ?> <?php the_time(); ?> <?php _e(‘by’); ?> <?php the_author(); ?>

    <?php if (is_search()) { ?>
    <?php the_excerpt() ?>
    <?php } else { ?>
    <?php the_content(__(‘Read the rest of this entry »’)); ?>
    <?php } ?>

    <p class=”feedback”>
    ” rel=”bookmark” title=”<?php _e(‘Permanent link to’); ?> <?php the_title(); ?>” class=”permalink”><?php _e(‘Permalink’); ?>
    <?php comments_popup_link(__(‘Comments’), __(‘1 Comment’), __(‘% Comments’), ‘commentslink’, __(‘Comments off’)); ?>
    <?php edit_post_link(__(‘Edit’), ‘ · ‘, ”); ?>

    <?php trackback_rdf(); ?>


    <?php endwhile; ?>

    <?php posts_nav_link(”, __(”), __(‘« Previous entries’)); ?>
    <?php posts_nav_link(‘ · ‘, __(”), __(”)); ?>
    <?php posts_nav_link(”, __(‘Next entries »’), __(”)); ?>

    <?php else : ?>

    <h2><?php _e(‘Not Found’); ?></h2>

    <?php _e(‘Sorry, but no posts matched your criteria.’); ?>

    <?php include (TEMPLATEPATH . “/searchform.php”); ?>

    <?php endif; ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Privacy based on levels’ is closed to new replies.