Support » Fixing WordPress » User-role based conditions without depracated $user_level?

  • Resolved SteelFrog


    I’m trying to write a sidebar where the user would have quick access to write and edit posts if the user level is greater or equal to 1.

    I check using the following snippet, but I’m told that $user_level is only kept in for legacy plugins.

    <?php global $user_ID, $user_email, $user_identity, $user_level; ?>
    <?php if ( $user_level >= 1 ) : ?>

    I’m trying to find document on how to do that now but can’t seem to find anything relevant other than the thread below which simply does not work for me.

    Is there anything else I can try? I simply need the user’s level, not the role.

    Running this from my admin account returns a user level of 0, while all other accounts are fine. From what I could find, this was an old issue with PHP4.X that was resolved in WP 2.7 or so, but I’m looking to make this future-proof.

Viewing 5 replies - 1 through 5 (of 5 total)
  • As I understand it, $user_level is deprecated because user levels are deprecated. That is, users aren’t going to have levels at some point in the future. All they will have are Roles and Capabilities. If you want to avoid deprecated code you’ll have to avoid user levels altogether and use Roles instead, which for some reason you appear not to want to do.

    Not at all, I’m open to change. I’m just not sure how to implement the roles since I couldn’t find any proper documentation on it. I mean I could write a snippet that would allow a list of roles to display certain items, but that doesn’t seem proper should the role names change.

    You should be able to get data on the current user using $current_user, if you get nothing from the var, then global first, global $current_user; and call get_currentuserinfo() to fill the var.

    An example:

    print '<pre>';
    print_r( $current_user );
    print '</pre>';

    .. to see what’s there, and failing that..

    global $current_user;
    print '<pre>';
    print_r( $current_user );
    print '</pre>';

    .. again just to see what’s there ..

    Marked as resolved. Thank you for your help! Much appreciated!

    This is an old post, but it keeps coming up in search results so I feel I should expand on my solution.

    You can check user’s permissions via current_user_can(‘var’). You can check the Roles page for variables.

    For example, if you want to check whether the current user is on a page or post, and has the rights to edit it and return a link to edit the post in a list:

    <?php if (is_single() or is_page() && current_user_can('edit_post')) { edit_post_link('Edit post', '<li>', '</li>') ;} ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘User-role based conditions without depracated $user_level?’ is closed to new replies.