Web & Software Developer

List All Roles With Their Capabilities in WordPress

This is how to get an array of all the existing user roles, and the capabilities for each role, in WordPress. If you don’t want to print it to the screen, omit the last line. The $roles variable on line 2 will hold the array of users and capabilities so that you can use it however you need to. See below for an example of the returned array.

global $wp_roles;
$roles = $wp_roles->roles; 

// print it to the screen
echo '<pre>' . print_r( $roles, true ) . '</pre>';

Example output

If you use the code above on a template file, it will print something like this on the screen. This is an array of all the user roles on a default WordPress site, along with all the user capabilities assigned to each user. If you’ve added any custom user roles or capabilities, they will also be included.

Array
(
    [administrator] => Array
        (
            [name] => Administrator
            [capabilities] => Array
                (
                    [switch_themes] => 1
                    [edit_themes] => 1
                    [activate_plugins] => 1
                    [edit_plugins] => 1
                    [edit_users] => 1
                    [edit_files] => 1
                    [manage_options] => 1
                    [moderate_comments] => 1
                    [manage_categories] => 1
                    [manage_links] => 1
                    [upload_files] => 1
                    [import] => 1
                    [unfiltered_html] => 1
                    [edit_posts] => 1
                    [edit_others_posts] => 1
                    [edit_published_posts] => 1
                    [publish_posts] => 1
                    [edit_pages] => 1
                    [read] => 1
                    [level_10] => 1
                    [level_9] => 1
                    [level_8] => 1
                    [level_7] => 1
                    [level_6] => 1
                    [level_5] => 1
                    [level_4] => 1
                    [level_3] => 1
                    [level_2] => 1
                    [level_1] => 1
                    [level_0] => 1
                    [edit_others_pages] => 1
                    [edit_published_pages] => 1
                    [publish_pages] => 1
                    [delete_pages] => 1
                    [delete_others_pages] => 1
                    [delete_published_pages] => 1
                    [delete_posts] => 1
                    [delete_others_posts] => 1
                    [delete_published_posts] => 1
                    [delete_private_posts] => 1
                    [edit_private_posts] => 1
                    [read_private_posts] => 1
                    [delete_private_pages] => 1
                    [edit_private_pages] => 1
                    [read_private_pages] => 1
                    [delete_users] => 1
                    [create_users] => 1
                    [unfiltered_upload] => 1
                    [edit_dashboard] => 1
                    [update_plugins] => 1
                    [delete_plugins] => 1
                    [install_plugins] => 1
                    [update_themes] => 1
                    [install_themes] => 1
                    [update_core] => 1
                    [list_users] => 1
                    [remove_users] => 1
                    [add_users] => 1
                    [promote_users] => 1
                    [edit_theme_options] => 1
                    [delete_themes] => 1
                    [export] => 1
                )

        )

    [editor] => Array
        (
            [name] => Editor
            [capabilities] => Array
                (
                    [moderate_comments] => 1
                    [manage_categories] => 1
                    [manage_links] => 1
                    [upload_files] => 1
                    [unfiltered_html] => 1
                    [edit_posts] => 1
                    [edit_others_posts] => 1
                    [edit_published_posts] => 1
                    [publish_posts] => 1
                    [edit_pages] => 1
                    [read] => 1
                    [level_7] => 1
                    [level_6] => 1
                    [level_5] => 1
                    [level_4] => 1
                    [level_3] => 1
                    [level_2] => 1
                    [level_1] => 1
                    [level_0] => 1
                    [edit_others_pages] => 1
                    [edit_published_pages] => 1
                    [publish_pages] => 1
                    [delete_pages] => 1
                    [delete_others_pages] => 1
                    [delete_published_pages] => 1
                    [delete_posts] => 1
                    [delete_others_posts] => 1
                    [delete_published_posts] => 1
                    [delete_private_posts] => 1
                    [edit_private_posts] => 1
                    [read_private_posts] => 1
                    [delete_private_pages] => 1
                    [edit_private_pages] => 1
                    [read_private_pages] => 1
                )

        )

    [author] => Array
        (
            [name] => Author
            [capabilities] => Array
                (
                    [upload_files] => 1
                    [edit_posts] => 1
                    [edit_published_posts] => 1
                    [publish_posts] => 1
                    [read] => 1
                    [level_2] => 1
                    [level_1] => 1
                    [level_0] => 1
                    [delete_posts] => 1
                    [delete_published_posts] => 1
                )

        )

    [contributor] => Array
        (
            [name] => Contributor
            [capabilities] => Array
                (
                    [edit_posts] => 1
                    [read] => 1
                    [level_1] => 1
                    [level_0] => 1
                    [delete_posts] => 1
                )

        )

    [subscriber] => Array
        (
            [name] => Subscriber
            [capabilities] => Array
                (
                    [read] => 1
                    [level_0] => 1
                )

        )
)

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]