WordPress.org

Ready to get started?Download WordPress

Forums

Privacy based on levels (11 posts)

  1. mmrobins
    Member
    Posted 8 years ago #

    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.

  2. skippy
    Member
    Posted 8 years ago #

    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">';
    <?php
    get_currentuserinfo();
    if ($user_level > 0) {
    the_content('Read the rest of this entry &raquo;');
    } else {
    echo 'Please login to read this post!';
    } ?>
    </div>

  3. Kafkaesqui

    Posted 8 years ago #

    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).

  4. mmrobins
    Member
    Posted 8 years ago #

    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}
    else
    {show full post}

    or using if (strpos(the_category(), 'CategoryName''))

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

  5. Kafkaesqui

    Posted 8 years ago #

    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.

  6. mmrobins
    Member
    Posted 8 years ago #

    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.

    <?php
    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.
    </div>
    <?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>

    <div class="feedback">
    <?php wp_link_pages(); ?>
    <?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
    </div>

    <!--
    <?php trackback_rdf(); ?>
    -->

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

  7. sahithi4u
    Member
    Posted 8 years ago #

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

  8. skippy
    Member
    Posted 8 years ago #

    "messed up" in what way?

  9. sahithi4u
    Member
    Posted 8 years ago #

    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.

  10. skippy
    Member
    Posted 8 years ago #

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

  11. sahithi4u
    Member
    Posted 8 years ago #

    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(); ?>
    -->

    </div>

    <?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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic