Check user role before proceeding (4 posts)

  1. BandonRandon
    Posted 5 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,

  2. mrmist
    Forum Janitor
    Posted 5 years ago #

    I think from the PHP side you can use

    current_user_can ("editor")


  3. Jacob N. Breetvelt
    Posted 5 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

    Posted 5 years ago #

    Ideally you should be checking against a capability, Otto summed it up pretty well here.

    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