#31304 closed enhancement (invalid)
Add wrapper function to check if a given role exists
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.2 |
Component: | Role/Capability | Keywords: | |
Focuses: | Cc: |
Description
Currently, the only way to check if a user role exists in the database is to use get_role()
and check if the result is null
or not. While that isn't too complicated to do, I think it would be useful to have a simple wrapper function that performs this check so that developers can easily check for a role before manipulating it (adding capabilities, etc.).
Attachments (1)
Change History (4)
#1
@
8 years ago
- Resolution set to invalid
- Status changed from new to closed
Closing because this is unnecessary.
#3
@
8 years ago
In case this ticket awakes again, here's a summary from the discussion in the Slack Channel.
- Return strict and just the result as it evaluates to
bool
anyway. - Use
\WP_Roles::is_role( $role )
Example:
/**
* @param string $role
* @return bool TRUE if the role exists
*/
function role_exists( $role )
{
/** @var \WP_Roles $wp_roles */
global $wp_roles;
return $wp_roles->is_role( $role );
}
Note: See
TracTickets for help on using
tickets.
Adds role_exists() function that returns boolean true/false