Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#28468 closed defect (bug) (invalid)

wp_capabilities (wp_usermeta table) actually serialized array of user_role

Reported by: nukeface's profile Nukeface Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.1
Component: Users Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Setup: WP3.9.1 Dutch Multi-site
Plugin (activated but un-used): Advanced Custom Fields - Problem persists without plugin.

The user_role assigned to users of a blog gets stored in a weird format with an incorrectly named meta_key.

When searching for users from a certain user_role a little trickery has to be used to be able to find the correct users.

table wp_usermeta
[meta_key] => [wp_capabilities]
[meta_value] => [a:1:{s:13:"administrator";b:1;}]

Not only is this a weird way to store this value, with an incorrect meta_key assigned, in this case it's also an incorrect value.

This is the value assigned to a super-admin. So why is it labelled "administrator" instead of "super-admin"? And why is it stored with "wp_capabilities" and not something along the lines of "wp_user_role"?

Also, why is the value stored like this?

serialize(
    array(
        [administrator] => true
    )
)

Why not plain-text, like pretty much any value in the wp_usermeta table?

To get the correct users to be returned a query now has to be submitted with a meta_query like so:

$roles[ 'relation' ] = 'OR' ;
foreach( $user_roles as $role ){
    $roles[] = array(
        'key'       => 'wp_capabilities',
        'value'     => serialize(array($role=>true)), /* This value gets stored as: serialize(array(1){[$key]=>bool(true)) when it's created */
        'compare'   => '='
    );
}

Also, the record underneath (with every user apart from 'super-admin' roles) shows [meta_key] => 0, with super-admins: [meta_key] => 10. The usage of wp_user_level has been deprecated since WP3.0, so why is that still being used? (source: http://codex.wordpress.org/User_Levels).

When using custom user_roles with custom capabilities this is very much so annoying to work through. Especially since there appears to be no reliable way to get the users of a specific user_role.

Paired with the WP_User_Query class to get the data in mixed formats (of Object, StdClass and Array) and which doesn't properly get the meta-data promised in the codex when using 'all_with_meta' in the 'fields' key for the query, this gets really frustrating (source: http://codex.wordpress.org/Class_Reference/WP_User_Query - Return Fields Parameter).

Change History (2)

#1 @Denis-de-Bernardy
10 years ago

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

This is by design and is related to the desire to allow for multiple roles and per-user caps.

#2 @SergeyBiryukov
10 years ago

  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Summary changed from wp_capabalities (wp_usermeta table) actually serialized array of user_role to wp_capabilities (wp_usermeta table) actually serialized array of user_role
Note: See TracTickets for help on using tickets.