WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] User-role based conditions without depracated $user_level? (6 posts)

  1. SteelFrog
    Member
    Posted 4 years ago #

    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.

    http://wordpress.org/support/topic/275681?replies=19

    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.

  2. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  3. SteelFrog
    Member
    Posted 4 years ago #

    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.

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    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;
    get_currentuserinfo();
    
    print '<pre>';
    print_r( $current_user );
    print '</pre>';

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

  5. SteelFrog
    Member
    Posted 4 years ago #

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

  6. SteelFrog
    Member
    Posted 3 years ago #

    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>') ;} ?>

Topic Closed

This topic has been closed to new replies.

About this Topic