Check if a WordPress user has a role or capability
Use current_user_can, user_can and a safe role check in PHP, why capabilities beat roles, and the timing mistakes that cause fatal errors.
At a glance
To check what a user may do in WordPress, call current_user_can with a capability such as edit_posts or manage_options, and use user_can for another user. Prefer capabilities to role names, call the check inside a hook after WordPress loads the user, and add a nonce check for any action that changes data. This guide includes code for each case.
Updated November 29, 2026
- For WordPress developers and plugin authors
- Core calls current_user_can, user_can, wp_get_current_user
- Prefer capabilities over role names
- Timing call inside init, admin_init or later hooks, not at file load
- Protect actions capability check plus nonce
- Testing one test user per role, plus a logged-out visit
- Help WordPress development and plugin development
On this page
- How do roles and capabilities relate?
- Which check should I use?
- How do I check a capability for the current user?
- How do I check another user, not the current one?
- How do I check a role anyway?
- What are the timing mistakes?
- Is a capability check enough to protect a form or AJAX action?
- How do I add or inspect capabilities?
- Worked example: a reports page only some staff can see
- How do I create my own capability?
- Which mistakes do I see most?
- How should I test it?
- FAQ
To check what a user is allowed to do in WordPress, call current_user_can( 'capability' ). Check capabilities rather than role names whenever you can, and always call the function after WordPress has loaded the user.
How do roles and capabilities relate?
A capability is a single permission, such as edit_posts or manage_options. A role is a named bundle of capabilities: Administrator, Editor, Author, Contributor, Subscriber, plus any added by plugins such as Shop manager in WooCommerce. Users can also have extra individual capabilities, and some have more than one role.
Because of this, "is this user an Editor?" is a weaker question than "may this user edit other people's posts?" The second is what your code usually needs.
Which check should I use?
| Question | Use |
|---|---|
| May the current user do this? | current_user_can( 'capability' ) |
| May the current user edit this post? | current_user_can( 'edit_post', $post_id ) |
| May another user do this? | user_can( $user, 'capability' ) |
| Does the user hold this role? | A helper that reads $user->roles |
How do I check a capability for the current user?
if ( current_user_can( 'edit_posts' ) ) {
// The user may create and edit their own posts.
}For actions on a specific object, pass the ID. WordPress then applies ownership and status rules:
if ( current_user_can( 'edit_post', $post_id ) ) {
// May edit this particular post.
}Common capabilities:
| Capability | Typical meaning |
|---|---|
manage_options | Change site settings (administrators) |
edit_others_posts | Edit posts written by others (editors and up) |
publish_posts | Publish rather than submit for review |
upload_files | Use the media library |
manage_woocommerce | Manage WooCommerce settings (shop managers and administrators) |
How do I check another user, not the current one?
$user = get_user_by( 'id', 42 );
if ( $user && user_can( $user, 'publish_posts' ) ) {
// User 42 may publish.
}user_can() accepts a user object or an ID. Always check the user exists first, as get_user_by() returns false when it does not.
How do I check a role anyway?
Sometimes the role itself is the rule, for example showing a banner to a wholesale customer role. The roles are stored in an array on the user object:
function mysite_user_has_role( $role, $user = null ) {
$user = $user ? $user : wp_get_current_user();
return $user instanceof WP_User && in_array( $role, (array) $user->roles, true );
}
if ( mysite_user_has_role( 'editor' ) ) {
// Show something to editors.
}Notes:
- The strict
truethird argument toin_arrayavoids loose comparisons. $user->rolescontains every role a user has, not just the first one.- Logged-out visitors get a user with no roles and ID 0, so the check returns false rather than erroring.
- Prefix your function name (
mysite_) so it cannot clash with another plugin's.
Avoid current_user_can( 'administrator' ). It works because WordPress registers a capability with each role's name, but that behavior is a side effect, and it is easy to confuse with real capabilities.
What are the timing mistakes?
The current user is loaded after plugins are loaded. If you call current_user_can() at the top level of a plugin or theme file, you can get a fatal error or a wrong answer:
// Wrong: runs while the plugin file loads, user not available yet.
if ( current_user_can( 'manage_options' ) ) { /* ... */ }
// Right: run it inside a hook.
add_action( 'init', function () {
if ( current_user_can( 'manage_options' ) ) {
// Safe here.
}
} );Use init, admin_init, wp, template_redirect or later hooks, as suits your task.
Is a capability check enough to protect a form or AJAX action?
No. It answers "may this person?" but not "did this person mean to do this?". For any action that changes data, add a nonce check as well:
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'mysite' ), 403 );
}
check_admin_referer( 'mysite_save_settings' );Hiding a button from some roles is not security on its own. The code that handles the request must check the capability as well, because anyone can send a request without seeing the button.
How do I add or inspect capabilities?
wp cap list editor
wp user list-caps 42A plugin such as User Role Editor shows each role's capabilities in the dashboard and lets you change them.
Changing roles on a live site affects real people, so test on staging first. If your project needs custom roles, per-customer permissions or a members area, that is a job for WordPress development or custom plugin development.
Worked example: a reports page only some staff can see
Suppose you add an admin page for sales reports and want shop managers and administrators to see it, but not editors. First choose the capability that matches the intent. manage_woocommerce fits a store; manage_options fits a site setting. Use it in two places: when you register the menu, and again when the page renders.
add_action( 'admin_menu', function () {
add_menu_page(
__( 'Reports', 'mysite' ),
__( 'Reports', 'mysite' ),
'manage_woocommerce',
'mysite-reports',
'mysite_render_reports'
);
} );
function mysite_render_reports() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You are not allowed to view this page.', 'mysite' ), 403 );
}
echo '<div class="wrap"><h1>' . esc_html__( 'Reports', 'mysite' ) . '</h1></div>';
}The capability argument in add_menu_page() hides the menu from users without it. The check inside the callback protects the page if someone visits the address directly. Do both.
How do I create my own capability?
If no built-in capability matches, add a custom one to the roles that should have it. Capabilities are stored in the database, so do this once, on plugin activation, not on every page load:
register_activation_hook( __FILE__, function () {
$role = get_role( 'editor' );
if ( $role ) {
$role->add_cap( 'mysite_view_reports' );
}
} );Then check current_user_can( 'mysite_view_reports' ) as usual. Administrators do not gain a custom capability automatically unless you add it to that role too. Remove it on uninstall if you want a clean exit.
Which mistakes do I see most?
- Checking a role name where a capability would do, then breaking when a membership plugin changes roles.
- Calling the check at file load time, giving fatal errors or wrong results.
- Hiding a link and assuming that protects the action.
- Forgetting that multisite super admins pass almost every check, so testing as one hides problems.
- Adding a custom capability on every request instead of once.
How should I test it?
- Create
a test user for each role you care about.
- Log in
as each in a separate browser profile.
- Visit
the page and the direct URL.
- Check
a logged-out visit as well.
A short list like this catches nearly all permission mistakes before customers do.
Roles, permissions and members areas.
Read more02Plugin developmentCustom capabilities and features.
Read morePost a project and a person will scope it and quote in writing.
Should I check roles or capabilities?
Check capabilities where you can. Roles can be renamed, changed by membership plugins and combined, while a capability describes exactly what the user may do.
Link to this answerWhy does current_user_can cause a fatal error?
It was called too early, before WordPress had loaded the current user. Call it inside a hook such as init or later, not at the top level of a plugin file.
Link to this answerDoes current_user_can return true for super admins?
On multisite, a super admin passes almost every capability check. Keep that in mind when testing with an administrator account.
Link to this answerWhere should I add a custom capability?
Once, on plugin activation, using get_role()->add_cap(). Capabilities are stored in the database, so adding them on every page load is wasted work.
Link to this answerWhat is the difference between a role and a capability?
A capability is a single permission, such as edit_posts. A role is a named bundle of capabilities, such as Editor. Users can also have extra individual capabilities and more than one role.
Link to this answerHow do I check another user instead of the current one?
Use user_can with a user object or an ID, after checking that get_user_by returned a user, since it returns false when the user does not exist.
Link to this answerIs current_user_can administrator a good way to check for admins?
Avoid it. It works only because WordPress registers a capability with each role's name, which is a side effect. Use a real capability such as manage_options.
Link to this answerWhy do I get a fatal error calling current_user_can at the top of a plugin file?
The current user is loaded after plugins load. Call the check inside a hook such as init, admin_init, wp or template_redirect, not at file load time.
Link to this answerHow do I create a custom capability?
Add it to the roles that should have it once, on plugin activation, using get_role and add_cap. Capabilities are stored in the database, so adding them on every request is wasted work.
Link to this answerDo administrators get a custom capability automatically?
No. Add it to the administrator role too if you want them to have it. Remove it on uninstall if you want a clean exit.
Link to this answerHow do I inspect a role's capabilities?
Use a plugin such as User Role Editor, or WP-CLI: wp cap list editor and wp user list-caps with a user ID. Test role changes on staging first because they affect real people.
Link to this answerHow should I test permission checks?
Create a test user for each role, log in as each in a separate browser profile, visit the page and the direct URL, and check a logged-out visit as well. Remember multisite super admins pass almost every check.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.