add_action('init', function()Īdd_role('plugins_manager', 'Plugins Manager', array( WordPress also offers a complete API to customize the roles and their capabilities. Var_dump($role->has_cap('install_plugins')) // Prints TRUE add_action('init', function()Ĭhecks if a role has the required capability. Note the usage of the admin_init hook, as the function is not loaded yet on the init one. The list may be overriden with the editable_roles filter, so we should not rely on this function to get the complete roles list on a website. We can note that current_user_can uses this function. add_action('init', function()Ĭhecks if a specific user owns a capability. current_user_can()Ĭhecks if the current user owns the required capability. WordPress provides the following global functions to help us work with roles. Working with Roles & Capabilities: the WordPress API Access the API Roles are never hard-coded a role is only a capabilities wrapper, it only exists in the database. Wp_die(_('You do not have sufficient permissions to manage plugins for this site.')) Several default capabilities are hard-coded in the WordPress core.įor instance, when loading the plugin screen, it will check if the current user can manage plugins, by running the following code: if (!current_user_can('activate_plugins')) We have seen how roles are loaded and linked to users from there, WordPress is able to get the capabilities of a given user, when needed. It was used to handle roles in old WordPress versions, and is now deprecated. This rule also applies to the wp_user_roles entry that we have seen before, in the wp_options table.įinally, we can see the wp_user_level meta along with the role. On a multisite installation, this would allow a user to use different roles on different instances: (We can get it by using the $GLOBALS->get_blog_prefix() function). Note that WordPress uses an array, although a user can only have one role at a time, we will see later why.Īlso, keep in mind that the wp_ part of the meta key is the prefix of the current blog. Once unserialized, the meta looks like this: array( WordPress uses a meta_key, stored in the wp_usermeta table, to link a user to his role. This occurs between the plugins_loaded and the init hooks. When WordPress starts, the WP_Roles class loads the list from the database. This meta is automatically set when installing a new WordPress site. The unserialized data looks like this: array( It uses the serialized wp_user_roles key. The database stores this list in the wp_options table. The list of default roles and capabilities is available on the WordPress Codex.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |