Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#43420 closed defect (bug) (wontfix)

Individual role array is $wp_roles->roles does not contain role slug

Reported by: subrataemfluence's profile subrataemfluence Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.4
Component: Role/Capability Keywords:
Focuses: Cc:

Description

I don't find role in dump using the following

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

foreach( $roles as $role ) {
   var_dump( $role );
}

var_dump( $roll ) shows this:

array(2) { ["name"]=> string(11) "News Editor" ["capabilities"]=> array(3) { ["read"]=> bool(true) ["edit_posts"]=> bool(true) ["publish_posts"]=> bool(true) } }

On the other hand var_dump( get_role( 'news-editor' ) ); retrieves the WP_Role object for the specified role dump comes up with the slug (news-editor).

Question is why each role array inside $wp_roles->roles does not include the slug. If I have to iterate through all roles and do something with respective slug part of each role WordPress doesn't provide it for me. There could be many cases where I cannot directly use get_role.

Can the role_objects array in init_roles function be generated with slug value of each role?

foreach ( array_keys( $this->roles ) as $role ) {
    $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
    $this->role_names[ $role ] = $this->roles[ $role ]['name'];
}

May be this has done intentionally, but getting role slug inside array might be helpful.

Change History (2)

#1 @soulseekah
6 years ago

The slugs are the keys, just use them in your loop.

foreach ( $wp_roles->roles as $id => $role ) {
    var_dump( $id );
    var_dump( $role );
}

Seems redundant and unnecessary?

#2 @johnbillion
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Yep, the array keys contain the role slug.

Note: See TracTickets for help on using tickets.