ViewLevel 2.0

ViewLevel 2.0 lets you hide specific posts from users below a given user level. This is a port of Kendra Burbank’s excellent ViewLevel plugin to Wordpress 1.5.

There is a new options page for setting the default viewlevel. Filtering is now done at the SQL stage, so unprivileged users now see the top ten viewable posts. In Kendra’s version, the filtering was done in the loop, so sometimes casual viewers might not see any posts at all, even when they should have been able to.

Latest version: 2.0.0
Released on: 25th August, 2005

Download it here: http://blog.firetree.net/wp-content/plugins/viewlevel20.zip

ViewLevel 2.0 is compatible with WordPress 1.5.2. (’Natures Revenge’ has e-mailed me to say that it works on WordPress 2.0, but I can’t personally confirm this.) No hacks or source code editing are required. If you have any questions or comments, then please subscribe to the mailing list.

Instructions

When posts have a custom field called “viewlevel” set, only lets users with that user level or above see the post.

As a shortcut, you may add text like “vl=3″ into posts to set the viewlevel. If you want to make a post visible to everyone, then use “vl=*”. (If you actually want to say “vl=3″ in your post, then use an ‘entity reference’ for the ‘=’, like this: “vl=3″.)

If you don’t set the viewlevel, then the post is assigned a default. You can set this default on the new ViewLevel Options page. To start with the default is ‘*’, so everyone will be able to see your posts.

Optional template change

If you want to mark restricted posts with the padlock icon, then change your index.php so that the post title part looks like this:

 <h2 class="posttitle" id="post-<?php the_ID(); ?>">
  <a href="<?php the_permalink() ?>" rel="bookmark">
   < ?php if(function_exists('vl_restricted'))vl_restricted(); the_title(); ?>
  </a>
 </h2>

The crucial part is the if(function_exists('vl_restricted'))vl_restricted();. This calls the new vl_restricted() template function if it exists.

vl_restricted() takes a couple of optional parameters that you can use to customise what it does. Read to code for further details.

Frequently Asked Questions

Q: Can I restrict posts to only registered users?

A: Yes. As of v2.0.0 you can set a post to ‘vl=*’ (or ‘vl=-1′), which means that everyone can see the post. ‘vl=0′ means that only registered users can see the post.

Q: Can I display a message when a post is hidden?

A: No! The restricted posts are filtered out at the SQL level, so they are completely hidden, as though they were never there. Some users like this feature, but not everyone. If you want to let your users know when something is hidden, then you’ll have to find a different plugin. Sorry.

Known Bugs

Comment feeds for restricted posts are visible to everyone. (Unconfirmed)

RSS

  1. WordPress Station » Blog Archive » ViewLevel 2.0 said,

    25 August, 2005 @ 19:18

    [...] firetree.net has announced a new plugin that allows for the admin to set posts to be viewed only by registered users, and by user level. Could be quite handy in a community oriented site, where various levels of user is used. Only concern regarding a plugin such as this, is when 1.6 is released, how will it work with the new permissions scheme, vs user level. Hopefully the plugin author has kept up with the development, and this will either deprecate gracefully, or will be easily adapted to the new system. [...]

  2. Bert Degenaar said,

    30 August, 2005 @ 07:45

    Hi alex

    I have a question about your viewlevel pluggin. Our fire-department in holland has a website that is running on wordpress. On this webblog we want to hide a part of the website from the public. There are some things that only concern the firefighters self. Your pluggin works great on the posts but what we really need is this pluggin also works on the ‘pages’. We have protected these pages with a password but it isn’t save enough. I want to know is this a difficult fix for you to do ? If it is very easy for you and not to much time consuming to do we like you ask to take a look at this request. You really would help us with it.

    Very kind regards Bert Degenaar from a dutch firedepartment in holland.

  3. Greg Elin said,

    5 September, 2005 @ 06:21

    Kendra – Thanks for the plugin. Does ViewLevel 2.0 remove hidden posts from feeds? From my testing it does appear to remove hidden posts. I just want to confirm with you. Thanks.

  4. alex said,

    5 September, 2005 @ 08:11

    Greg Elin: Erm. Alex here, not Kendra. Yes ViewLevel 2 does remove hidden posts from feeds.

  5. ana said,

    7 September, 2005 @ 18:59

    Hello! Thank you for a great plugin!

    Like so many commenters on this kind of post I come to you with a question. :)

    I wish to protect all the of the posts already existing in the WP blog I installed your plugin in– basically, right now the blog is behind an htaccess file, and I would like to get rid of it, mostly so xmlrpc.php is accessible to the software I need accessing it. Your plugin is perfect for that, since I can now make the whole blog virtually invisible to non registered users without needing the htaccess pwd protection.

    I understand I can set the default for future posts (and I did, to vl=0). I understand how to protect older posts by editing them one by one to add the vl=0 I need in a new field I called ‘viewlevel’ – the new field shows in the dropdown for user generated fields and all that. But I have more than 500 posts to edit… I was wondering if there is a script I could run to make that edit in one massive go, or a line of command I could fede the DB through a shell or whatever. (I know just enough to imagine there is a solution to my problem, but not what the solution could be, sadly.)

    Any idea for me? Sorry to ask for your help like this… It’s a great plugin no matter what, and I’m grateful to have found it. :-)

  6. alex said,

    7 September, 2005 @ 23:42

    ana: You don’t need to do anything to older posts (or new posts) to give them the default viewlevel. They just have it.

    So I think you don’t need to do anything else. Once you’ve set the default level to “registered only” (0) in the options screen, just log out and you will see that all of your old posts have gone. Log back in again, and there they are!

    The default view level is not added to new posts as a custom field. It is just applied to all posts that have no viewlevel custom field. So all of your old posts will count.

  7. Steve said,

    13 September, 2005 @ 21:10

    Wow, cool plugin, but I am having some trouble doing what I want.

    I can’t seem to change the default level. I change it to zero, click submit, and it changes back to -1 (or *). ???

    My goal is to make my entire blog only accessible to people who register.

    I tried with linux firefox, windows firefox and windows IE, just to see if it was a browser combatability issue.

    A possible cause of my trouble is that I just upgraded WP to 1.5.2 minutes before installing your plugin, so could it be a permissions issue on one of the files?

    Any ideas?

  8. alex said,

    13 September, 2005 @ 21:45

    Steve: Hmm. I don’t understand why that doesn’t work for you. The setting is stored in the table wp_options as an option called “vl_default”. Perhaps you don’t have permission to write to the database? Do you see any error messages (on the screen or in the HTML)?

    If you can write to the database from the SQL command line, then something like this will set it (provided that the option already exists):

    SQL> update wp_options set option_value='0' where option_name='vl_default';

    Let me know how you get on.

  9. Laurabelle said,

    14 September, 2005 @ 18:26

    Hi Alex,

    Great plugin! It does exactly what I want, except that comments on restricted posts are published in my comments feed. I noticed that your plugin has code that’s supposed to restrict access to the comments feed only to authorized users, but that doesn’t seem to be working. I don’t really like that solution anyway, as I have people who use my comments feed through clients that don’t allow them to authenticate.

    I’m willing to take a crack at finding a better solution to the problem, but I don’t want to duplicate effort since I assume you’ve probably already explored other options and found them wanting. Do you see any other possible solutions?

    Thanks, Laura

  10. marcus said,

    15 September, 2005 @ 14:11

    hey alex.

    seems to be a nice plugin. my question: does this work for pages as well?

    thanks, marcus

  11. alex said,

    15 September, 2005 @ 15:06

    marcus: It does work with pages, but it’s not as smooth as I would like. The vl=X syntax doesn’t work with pages, so you have to manually set the viewlevel custom field. Also the automatically generated page list always lists all of the pages – you just get a 404 when you try to view a restricted page.

  12. marcus said,

    15 September, 2005 @ 16:37

    thanks for answering. good, gonna give it a try. i think it will fit my needs though.

    m

  13. alex said,

    16 September, 2005 @ 14:02

    Laurabelle: Thanks for the bug report. I’ll put it on the list. If you do fix it before I get there, then let me know.

  14. quayso said,

    21 September, 2005 @ 19:56

    Thank you so much!!!!!!! This is exactly what I needed.

    One question, the padlock icon doesn’t come up when the view level is zero for a post. Can this be changed?

  15. alex said,

    21 September, 2005 @ 20:00

    quayso: Ooops. Sounds like an oversight. I’ll post back when I’ve fixed it.

  16. quayso said,

    21 September, 2005 @ 22:18

    One other question (again, I love the plug-in):

    I am using the get_archives to list my five post recent entries, and all my entries are showing up — even if I am not logged in and there are no unlocked posts. Of course, once I click on the link (if I’m not logged in) the plug-in does its magic and I can’t read the post. Is there a way to keep the links to recent entries of protected pages off the “recent entries”?

    If there isn’t a trivial way to do this, then I don’t much mind. I just thought I’d point it out in case it were something easy to change.

  17. jason said,

    23 September, 2005 @ 19:15

    This works wonderfully for something we’re planning on using to do interna/external work. Since this post/thread is more up-to-date … so I’m re-posting here from what I posted at the previous post on viewlevel2 (beta) …sorry for the dupe.

    Thus far on my install, this works at not allowing unregistered users to not see the posts, but any registered user can see it now, no matter the number in vl=# … And it only seems to work with the custom field … not with vl=# right in the post text.

    I have the viewlevel at 8, but a user at level 0 can see it. Am I missing something? Also, I am on WP 1.5.2. Anyone have it working with 1.5.2

  18. alex said,

    23 September, 2005 @ 19:23

    Jason: The vl=# shortcut only works when you Publish the post. After that it’s ignored. If you want to go back and change the viewlevel (as you will if you add the plugin to an existing site) then you have to use the normal method of adding the ‘viewlevel’ custom field.

    Sorry, that’s an oversight. I’m intending to fix it in the next version.

  19. jason said,

    23 September, 2005 @ 19:37

    Thanks Alex …

    So let me see if I understand this right. To limit the viewlevel, I put in a custom field titled viewlevel and the value of that field is vl=# (where # is the level I want users to have a minimum of to view it). I have been setting it up using the field as viewlevel and the value as vl=#.

    Thanks for the tip on the vl=# inside the post trick.

  20. jason said,

    23 September, 2005 @ 19:42

    Nevermind … after I wrote that last comment, I did a little more trial and error and figured it out. Somehow I misread the viewlevel custom field set up the first few times around (It is Friday aftenoon)

    Great Work!

  21. jason said,

    27 September, 2005 @ 19:24

    One thing I just noticed is that it will highlight the day in the calendar of a restricted post when viewed by a ‘non-authorized’ viewer. Clicking through produces a ‘no results’ result. I’m interested in learning more about plug-ins with WP, so I’m poking around a little to see how to resolve it … Anyone else notice the same behavior?

  22. johnny said,

    28 September, 2005 @ 08:21

    I tried to add your padlock code but couldn’t get it to work

    here is my post.php file. [garbled]

  23. alex said,

    28 September, 2005 @ 12:58

    quayso: Sorry, but get_archives() reads the database directly, and it doesn’t provide any plugin hooks. You’d have to write a whole new version of it if you want to have that functionality and still hide ‘hidden’ posts.

    jason: Yeah. Again, the calendar reads the database all on its own, and doesn’t provide any plugin hooks. This same thing happens on my website (not firetree.net – another one). For my site it’s not too bad, since we don’t really mind if visitors know that events are happening, we just want to hide the details.

    Some integration between EventCalendar and ViewLevel plugins would probably be good though. I don’t have time for it right now.

    jonny: Your example was garbled. Try emailing it directly to me.

  24. TedFox said,

    1 October, 2005 @ 09:09

    It’ll be nice that in addition to setting default view levels, how about an option page to set a different default level for different categories, with the viewing level at the lowest default level for all the categories an entry is in. (or maybe highest. haven’t quite thought about it)

  25. Edelholz said,

    3 October, 2005 @ 13:50

    Hey alex, this is already the second plugin you wrote that I really like. And this time, I don’t even have anything to complain about ;). Anyway, I just wanted to bring a little bug (?) to your attention. In viewlevel2.php, function vl_restricted(), you’re checking whether the viewlevel is above 0. But as the function is supposed to indicate all the posts that are only viewable by registered user (to my understanding), you have to check if the viewlevel is above -1, because when the viewlevel is set to zero for a post, only registered users can see it. So far, and thanks for all the fish :). Great plugin.

    my code excerpt: if($vl>-1) $result=$lockedtext; else $result=$notlocked_text;

  26. TedFox said,

    3 October, 2005 @ 16:27

    doesn’t work together with event calendar plugin? or rather. the event plugin doesn’t check for view levels?

  27. Scott said,

    10 October, 2005 @ 19:03

    Your functionality works for Page type posts too. With viewlevel 1 set for a Page, the user cannot view the page.

    However, the page is still listed in the sidebar. It’s be great if the page wasn’t even listed in the sidebar unless they were logged in and of a certain level.

    Nice plugin.

  28. Jim said,

    8 November, 2005 @ 14:30

    Could you compare / contrast this plugin with the category-levels plugin? Just wondering if your plugin has different features. Thanks, jim

  29. alex said,

    13 November, 2005 @ 20:50

    Comments are now closed, because I can’t keep up with support requests as blog comments. If you need help, then please subscribe to the new mailing list.

Sponsors