WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#31304 closed enhancement (invalid)

Add wrapper function to check if a given role exists

Reported by: hlashbrooke 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)

31304.diff (724 bytes) - added by hlashbrooke 7 years ago.
Adds role_exists() function that returns boolean true/false

Download all attachments as: .zip

Change History (4)

@hlashbrooke
7 years ago

Adds role_exists() function that returns boolean true/false

#1 @hlashbrooke
7 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Closing because this is unnecessary.

#2 @DrewAPicture
7 years ago

  • Milestone Awaiting Review deleted

#3 @F J Kaiser
7 years ago

In case this ticket awakes again, here's a summary from the discussion in the Slack Channel.

  1. Return strict and just the result as it evaluates to bool anyway.
  2. 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.