WordPress.org

Ready to get started?Download WordPress

Forums

Check user role before proceeding (4 posts)

  1. Brooke Dukes
    Member
    Posted 3 years ago #

    I am working on a plugin and would like to check the users role before a certain action. I know I can use current_user_can() but this requires a checking against something like "edit_post" which works fine if only used from the code side. I however want the admin to be able to select a role from a drop down (like the current user role drop down on the user page) and then check against that role.

    Something like:
    if(user_role()=="editor"){//do something }

    This should be less confusing. I know that older versions of WordPress used user roles as numbers with admin being 10 I think this has been depreciated.

    Any Help would be great,
    Brooke

  2. mrmist
    Forum Janitor
    Posted 3 years ago #

    I think from the PHP side you can use

    current_user_can ("editor")

    Etc..

  3. Jacob N. Breetvelt
    Member
    Posted 3 years ago #

    Yes, i just modified my plugin wp-photo-album-plus in this way and it functions as expected.
    You can see 'roles' as capabilities.

    this will also work in add_menu_page() and add_submenu_page()

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Ideally you should be checking against a capability, Otto summed it up pretty well here.
    http://lists.automattic.com/pipermail/wp-hackers/2010-January/029737.html

    Not vital that you check against capability, but it makes more sense to get into a routine of checking against a capability if you intend to write code that's going to be distributed to users (who may have custom roles).

Topic Closed

This topic has been closed to new replies.

About this Topic