• Hello

    When I try to access index zero of user role array, it doesn’t find because the role added by plugin is placed at index 4.

    $current_user->roles = Array( [4] => ‘product_manager’ )

    Many plugins, like Google Tag Manager, usually access the user’s role at index zero, generating incompatibility.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi,

    Thank you.
    I will reindex the array of roles granted to user after the modification of the list of roles/capabilities granted to a user.

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi again,

    Looking more thoroughly, I remembered that user->roles array indexing does not depend from the User Role Editor plugin. Look how WordPress builds the array of roles granted to user (file wp-includes/class-wp-user.php, function get_role_caps(), line #507:

    
    // Filter out caps that are not role names and assign to $this->roles.
    if ( is_array( $this->caps ) ) {
      $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
    }
    

    If you look at the php.net for the array_filter() function documentation, you find, that

    Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function.

    So if user has subscriber role, but it is 5th in the $wp_roles array, resulting $user->roles array will be looked this way array(4=>'subscriber').

    So primary role at the user’s roles array not always have the ‘0’ index. We have to use another way to get the 1st element of the roles array, than just a $user->roles[0].

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Incorrect array index in role added by plugin’ is closed to new replies.