From 0b98699012e34f8c23ec50b07745842e50309293 Mon Sep 17 00:00:00 2001 From: Mohd Salman <47385962+salmanshafiq8630@users.noreply.github.com> Date: Mon, 22 Jun 2026 13:16:57 +0530 Subject: [PATCH 1/3] Users: Introduce wp_get_user_roles() helper function Users: Add wp_get_user_roles() helper function. Introduce wp_get_user_roles() to provide a public API for retrieving the roles assigned to a user. The function returns an array of role slugs or an empty array if the user does not exist or has no assigned roles. Includes unit tests. --- src/wp-includes/user.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 0e491d666087a..0154f2a25eba6 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -5230,3 +5230,21 @@ function wp_is_password_reset_allowed_for_user( $user ) { */ return apply_filters( 'allow_password_reset', $allow, $user->ID ); } + +/** + * Retrieves the roles assigned to a user. + * + * @since 6.x.x + * + * @param int $user_id User ID. + * @return string[] Array of role slugs. Returns an empty array if the user does not exist or has no assigned roles. + */ +function wp_get_user_roles( $user_id ) { + $user = get_userdata( $user_id ); + + if ( ! $user instanceof WP_User || empty( $user->roles ) ) { + return array(); + } + + return $user->roles; +} From f88dafc5d8d7e20ac04f6bb0e6514bdf3c5e8a46 Mon Sep 17 00:00:00 2001 From: Mohd Salman <47385962+salmanshafiq8630@users.noreply.github.com> Date: Mon, 22 Jun 2026 13:23:38 +0530 Subject: [PATCH 2/3] Update user.php Change Since Version 7.1 --- src/wp-includes/user.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 0154f2a25eba6..0dcd1269ca526 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -5234,7 +5234,7 @@ function wp_is_password_reset_allowed_for_user( $user ) { /** * Retrieves the roles assigned to a user. * - * @since 6.x.x + * @since 7.1 * * @param int $user_id User ID. * @return string[] Array of role slugs. Returns an empty array if the user does not exist or has no assigned roles. From 17b7c8f77ed0f4013526ef247f0770de886b8e23 Mon Sep 17 00:00:00 2001 From: Mohd Salman <47385962+salmanshafiq8630@users.noreply.github.com> Date: Mon, 22 Jun 2026 14:04:38 +0530 Subject: [PATCH 3/3] Update the @since to 7.1.0 Remove the redundant empty check. --- src/wp-includes/user.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 0dcd1269ca526..ae6c9365e95df 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -5234,7 +5234,7 @@ function wp_is_password_reset_allowed_for_user( $user ) { /** * Retrieves the roles assigned to a user. * - * @since 7.1 + * @since 7.1.0 * * @param int $user_id User ID. * @return string[] Array of role slugs. Returns an empty array if the user does not exist or has no assigned roles. @@ -5242,7 +5242,7 @@ function wp_is_password_reset_allowed_for_user( $user ) { function wp_get_user_roles( $user_id ) { $user = get_userdata( $user_id ); - if ( ! $user instanceof WP_User || empty( $user->roles ) ) { + if ( ! $user instanceof WP_User ) { return array(); }