Support » Themes and Templates » Trying to limit categories by user level

  • Hello. I’ve been trying for two weeks now to get user-to-category rules working. I am trying to limit the categories users see in the blog by user level.
    I have the following code in my index.php in the top section:

    get_currentuserinfo();
    if ( $user_level == "0" || ! isset($user_level) ) { $cat = 1; }
    elseif ( $user_level == "1" ) { $cat = '1,2'; }

    As you can see, I want user_level 0 and no user level to have category 1, and user_level 1 to have categories 1 and 2. Everyone else gets to see it all. My index.php is basically stock other than that – I don’t want to intrude any more than absolutely necessary. I did put an echo in my index.php file to see what I’m getting, and according to the echo, when logged in at level 1, I have cat = ‘1,2’ … but, I am seeing all the categories on the page. Does anyone have any ideas why I’m seeing categories I shouldn’t, and how I can improve the code above to do the user-to-category limiting? Thanks…
    Update I just noticed that the cat= has to be above the wp-blog-header.php call, but I cannot get the user_level or get_currentuserinfo() until after the wp-blog-header.php call. How can I make this work? Can I somehow access the COOKIE directly? How would I get the hash on the cookie name? I’m starting to think this isn’t going to be possible. Anyone have an idea?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I finally figured this out…it’s amazing what a little Stoli will do for you…er…me. I honestly don’t know if this is the “correct” solution, but it works.
    Here is the top of my index.php file now, after my “hacks”:

    <?php
    /* Don't remove these lines. */
    $blog = 1;
    require('wp-config.php');
    get_currentuserinfo();
    if ( $user_level == "0" || ! isset($user_level) ) { $cat = 1; }
    elseif ( $user_level == "1" ) { $cat = '1 2'; }
    require('wp-blog-header.php');

    I looked around the code some more, and found that I needed the wp-config.php file code in order to get the get_currentuserinfo() function. So, I just started playing, and TaDa! I have something that works. And it should be pretty portable to “updated” versions of index.php. It would be really nice if this ability (obviously not this code) could be considered for inclusion in WP, but my simple hack will do what I am looking to do.
    One other note: I saw a lot of references to $cat='1,2' in other posts, and that was really messing me up, as the proper syntax is like I have it above – $cat='1 2' – with the space and not the comma. Just thought I would point that out for anyone else having problems.

    Hi Daddio,
    Thanks for this hack! I searched for a solution to protect some categories with passwords. I think to limit access by user-level is much better.
    One thing is still missing: The categories users with level 0 are not allowed to see should not appear in the list. But it may not be a big problem.
    oerdec//

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trying to limit categories by user level’ is closed to new replies.