ViewLevel 2.0 beta

The release candidate of ViewLevel 2.0 is now available. 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.

Update!

ViewLevel 2.0 has now been released.

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

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

Read the latest documentation.

Beta Documentation:

ViewLevel 2.0 is compatible with WordPress 1.5.1.3. No hacks or source code editing are required. If you have trouble, first make sure you are using the very latest Wordpress code.

Please try this beta plugin, and let me know what you think. If you find a bug, then first check in the list below to see if it’s already been reported or fixed. If not, then please report it by posting a comment.

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 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 ‘0′, 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.

Bugs so Far

  • Doesn’t work! Low user level users get SQL errors, and can’t see any posts. Thanks to everyone who reported this problem. (fixed in rc2)

Comment · RSS · TrackBack

  1. gaba said,

    22 July, 2005 @ 19:06

    First of all: Thanks for your great work!

    I installed the plugin, but it did not work on my server, which had PHP 4.3.10, WordPress 1.5.1.3 and MySQL 4.0.24.

    There was an error in the SQL the plugin has generated. It contained errors like this: …AND ID IN (Object,Object,…)…

    The query string was compiled in function function vl_filter_where() via the expression: implode(',',$post_list). The array $post_list contained objects indeed, and these objects had the field post_id.

    My solution was:

    Added a new function:

    /** Returns an array containing the post_ids of the posts in $post_list */
    function vl_get_post_ids( $post_list ) {
        $post_ids = array();
        foreach ( $post_list as $post_entry )
            $post_ids[] = $post_entry->post_id;
        return $post_ids;
    }
    

    Changed two lines in your code, where implode() was used:

    $and_where = " AND ID NOT IN (".implode(',',vl_implode_post_ids($post_list)).")";

    and

    $and_where = " AND ID IN (".implode(',',vl_get_post_ids($post_ids)).")";

    Now it works fine on my server too.

    Sorry if my code is not perfect, I normally use Java and C#.

  2. gaba said,

    22 July, 2005 @ 19:15

    My comment appeared quite weird, so I posted it on my blog as well: http://www.netacademia.net/blogspace/gaba/

  3. gaba feljegyzései said,

    22 July, 2005 @ 19:40

    ViewLevel 2.0 RC1

    I installed version 2.0 RC1 of the ViewLevel WordPress plugin, but it did not work on my server (which had PHP 4.3.10 and WordPress 1.5.1.3). There was an error in the SQL the plugin generated. It contained errors like this: …AND ID IN (Object,Object…

  4. gaba feljegyzései said,

    22 July, 2005 @ 19:40

    Tûzfablog

    A WordPress-alapú projektemhez kerestem olyan plugint, ami bizonyos bejegyzéseket csak megfelelõ felhasználói szint felett mutat meg. Tudtam a ViewLevel 1.1.2-rõl, de most kiderült, hogy valaki megcsinálta az 1.5-ös WordPresshez igazított vá…

  5. alex said,

    23 July, 2005 @ 21:10

    gaba: Thanks for you code. I actually had some thing like that already!! I took it out just before I released RC1 because it didn’t seem to be needed. Perhaps it is only needed by certain versions of PHP.

    Anyway, I’ll put it back in. Thanks again.

  6. indi said,

    23 July, 2005 @ 21:20

    I get an odd error in Internet Explorer only:

    WordPress database error: [Unknown column 'Object' in 'where clause'] SELECT DISTINCT * FROM wpposts WHERE 1=1 AND postdate_gmt

  7. indi said,

    23 July, 2005 @ 21:36

    yes that fix works. Didn’t notice the timestamps were so close, sorry to repeat an attended to concern

  8. Chris said,

    25 July, 2005 @ 10:21

    I’m getting errors like this at the top of the page when attempting to view a category from a non-authorized user (not logged in, trying to view a private post and/or category)

    WordPress database error: [Unknown column 'Object' in 'where clause'] SELECT DISTINCT * FROM wpposts LEFT JOIN wppost2cat ON (wpposts.ID = wppost2cat.postid) WHERE 1=1 AND (categoryid = 3) AND postdategmt

  9. Bath said,

    27 July, 2005 @ 12:02

    i am having the same issue as above, a pasting of the error is below:

    WordPress database error: [Unknown column 'Object' in 'where clause'] SELECT DISTINCT * FROM wpposts WHERE 1=1 AND postdate_gmt

  10. alex said,

    30 July, 2005 @ 10:46

    I’ve now released v2.0.rc2 which fixes this glaring error that you’ve all reported. Sorry that the fix took so long – I’ve been away!

  11. Bath said,

    30 July, 2005 @ 14:16

    Top work Alex! Just got the latest release and installed this morning and it all seems to be running sweetly!! Thanks muchly for your time and energy. Hope you had good hols.

  12. Rafael Bravo said,

    1 August, 2005 @ 07:56

    Wow! You don´t know how thankful i am! This plugin is crusial for my blog! many thanks!

    rafael

  13. Westermeyer Portal » Restricted News said,

    1 August, 2005 @ 15:06

    [...] We have just added a feature to this site. With this new feature you can now add news to the site that is only meant to be shared with family and friends and not necessarily with the whole world. So here is how this works. [...]

  14. gaba feljegyzései said,

    2 August, 2005 @ 13:57

    ViewLevel 2.0 RC2

    Alex Tingle javította az általam is jelzett hibát az RC1-ben, így elérhetÅ‘ a ViewLevel 2.0 RC2. Ennek a WordPress pluginnak a segítségével kényelmesen beállíthatjuk, hogy blogunk egyes bejegyzéseinek elolvasásához milyen felhasználói…

  15. Olli said,

    2 August, 2005 @ 22:10

    Nice plugin, I have a question though. With the new userlevels as of 1.5 whenever you raise someones ‘level’ they gain also other priviledges I don’t want them to have.

    As of userlevel 1 they are already capable of submitting Drafts and the like, I’m not gonna even consider using more user levels since it will open up way to much capabilities to my readers.

  16. gaba feljegyzései » archívum » Tűzfablog said,

    5 August, 2005 @ 01:02

    [...] A WordPress-alapú projektemhez kerestem olyan plugint, ami bizonyos bejegyzéseket csak megfelelő felhasználói szint felett mutat meg. Tudtam a ViewLevel 1.1.2-ről, de most kiderült, hogy valaki megcsinálta az 1.5-ös WordPresshez igazított változtatot, a ViewLevel 2.0-át (egyelőre RC1 állapotban van). [...]

  17. warmac said,

    6 August, 2005 @ 19:13

    i notice when you click on a post with viewlevel specified it displays a “Not Found” is that correct ?

    is there anyway to display a “post restricted” instead ?

  18. warmac said,

    6 August, 2005 @ 19:20

    i’m not clear on the padlock, you say to replace the code above with the title code in our index page ? this is the error that shows up in place of the post title

    or did i miss something ?

  19. warmac said,

    6 August, 2005 @ 20:16

    here’s what my code reads in the index.php ( i removed the brakets because i wasn’t sure it would display here ) [I tried to put them back -Alex]

    <h1 id="post-<?php the_ID(); ?>">
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title(); ?>">
        < ?php the_title(); ?>
    </a></h1>
    

    so if we replace it with the code you suggested above will it show the padlock ?

  20. alex said,

    7 August, 2005 @ 01:05

    warmac: Yes, that’s the code that you need to replace. Make sure that there is no space character between the ‘<’ and the ‘?’.

  21. warmac said,

    7 August, 2005 @ 03:31

    how do I know if its working ? the posts has a field with “not found”

  22. Gill said,

    7 August, 2005 @ 04:54

    This plugin is brilliant. That’s all I have to say. :)

  23. alex said,

    7 August, 2005 @ 09:03

    Gill: Thanks. Glad you’ve found it useful.

    Warmac: Make sure that the plugin is activated. Make a post with ‘viewlevel’ greater than zero. Then look at the post on the front page – there should be a padlock icon where you put the call to vl_restricted() in your template.

  24. warmac said,

    7 August, 2005 @ 09:41

    i have it setup exactly as per the instructions and still don’t see a padlock or the posts all that’s there is a form field with the words “not found”

  25. Eric said,

    9 August, 2005 @ 20:48

    Anyway I can configure it so it shows:

    “Sorry, you need to be logged in to see this post.”

  26. kalliope said,

    11 August, 2005 @ 15:30

    I’ve installed rc2, and it’s not working for me. The “hidden” posts don’t show up for anyone, regardless of user level (not even to me, the admin w/ user level 10).

    I have the most recent version of WP (1.5.1.3), MySQL v 4.0.24-standard, PHP v 4.3.11.

    I’ve had the same problem with the Category Levels plug-in, so I’m not sure where the problem lies. Anyone have any suggestions?

  27. monkeypup said,

    11 August, 2005 @ 19:56

    Loves it! Thanks so much. This helped me with an issue I had for a work site that only allows certain folks to see posts. Works like a charm!

  28. kalliope said,

    11 August, 2005 @ 22:30

    I’ve figured it all out — I had things listed strangely in my cpanel due to my add-on domain. Everything is working properly now, thanks!

  29. Tom said,

    19 August, 2005 @ 20:10

    Great plug in!

    Got a question, how can I lock every post out so only those who are registered can see them?

    Can I code the template for this?

    Thanks

    T

  30. Ross said,

    21 August, 2005 @ 18:33

    Obviously the code only allows posts to be retrieved for the user level with which you have permissions.

    However, how can I show the title of the post and a ‘You’re not authorised to view this post’ message for those that I don’t have access to?

    Seems like this is not catered for as it returns the post IDs from the database, therefore cannot manipulate the restricted posts as they are not in the result set.

    Any plans to add this capability?

  31. Ross said,

    21 August, 2005 @ 19:04

    OK, had a look through the code and came up with this:

    Comment out the following line at the top of viewlevel2.php //add_filter('posts_where', 'vl_filter_where');

    Add the following code which filters the content output so that the title is still shown, but not the actual post itself.

    add_filter('the_content', 'vl_filter_content');
    
    function vl_filter_content(
      $content='',
      $private_message = 'You are not authorised to view this content')
    {
        get_currentuserinfo();
      global $user_level;
      if(isset($user_level))
          $my_user_level=$user_level;
      else
          $my_user_level=0;
      
      $vl=intval( vl_viewlevel() );  // get view level of current post
      
      if($vl > $my_user_level) // show not authorised message if view level greater than user level
        $content=$private_message;
      
      return $content;  
    }
    

    You can still retain the neat padlock against the title too. I like this approach better since it means users know they are missing out on something.

  32. alex said,

    21 August, 2005 @ 20:16

    Olli, Tom: Yes I think it would be a good idea to have a viewlevel for users who have only registered. I don’t know how easy that will be, but I’ve added it to my ‘to do’ list.

    Eric: No, you can’t make it display a special message when there are invisible posts. That’s because it removes the posts at the SQL level, so Wordpress doesn’t even know they are there. Sorry.

    Ross: Your approach is very similar to that from the original v1.0 plugin. I didn’t like it because often unregistered visitors would be presented with “Nothing to see here”, when in fact there were posts that they could see, just not in the first 10 (or whatever). If you prefer your approach then go for it. I don’t plan to put that feature into my version.

  33. Hanna said,

    22 August, 2005 @ 13:06

    Looks like a good plugin, I’ll be using it. :)

    I hope Ross’ suggestion of showing users that there is a post and that they don’t have access to it, won’t get incorporated into the plugin, because to me, the whole point of using something like this instead of just password-protecting posts, is to hide the existance of the posts I want protected completely. Only people with high enough level should ever know that the posts are there.

    Hopefully something like this will be incorporated into the core of wordpress in the future.

  34. alex said,

    25 August, 2005 @ 14:47

    Update!

    ViewLevel 2.0.0 has been released.

    http://blog.firetree.net/2005/08/25/viewlevel-20/

  35. jason said,

    23 September, 2005 @ 18:01

    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’m on WP 1.5.2, was this tested against 1.5.2?

  36. quayso said,

    27 September, 2005 @ 00:28

    I figured out why no padlock was displaying when the viewlevel was set to 0.

    All you have to do is change:

    if($vl>0) $result=$locked_text;

    to

    if($vl>=0) $result=$locked_text;

    It’s working for me, so I hope that’s truly all it takes! :)

  37. alex said,

    28 September, 2005 @ 12:44

    quayso: Thanks for that. I’ve updated it and the fix will be in the next release.

  38. Jasper said,

    15 October, 2005 @ 22:33

    Hi,

    Am using your plugin. Haven’t used the padlock yet cause my post snippet is different. Might ask help for that later. But my question now is of a different order. I set the viewer level for a post and I can see it when I am logged in. But I tried to change it back so everybody can see it again. If I added vl=* and checked after I logged off it still wasn’t there! How come? Do I need to adjust the post in the database or something?

    Thanks for your help.

    Cheers,

    Jasper

  39. pjn said,

    17 October, 2005 @ 08:33

    Jasper : Had a similar problem. use a custom value “viewlevel” with a value of “-1″. Finally made it work for me.

    Another thing. People are still able to see how many posts there are in the different categories and archives, even though their contents are not displayed.

    I would also like it to hide links according to userlevel (Blogrolls etc):-)

    Or would it just be better if the wordpress-people actually made a somewhat working user management?

  40. jonathan said,

    30 March, 2006 @ 13:24

    I have a problem that was first mentioned here: http://wordpress.org/support/topic/11469

    and that is that I set an article with vl=0 but unregistered users can still see it, I set it with 1 and they can’t see it and nor can anyone else including the administrator (me)

    I have the deafult set as *-

    I am not sure why this is happening, can you point me in the right direction?

  41. Ron said,

    23 March, 2007 @ 18:18

    Hi there- Will there be an updated version for WP 2.1?

Leave a Comment

Sponsors