Make WordPress Core

Opened 5 months ago

Last modified 3 weeks ago

#60579 new defect (bug)

Notice: Undefined index: name in /wp-includes/class-wp-roles.php on line 293

Reported by: mrsize's profile mrsize Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 6.4.3
Component: General Keywords: has-patch
Focuses: Cc:

Description

http://servtest.ovh/notice-wp.jpg

I have some notices on my dashboard, and fix the bug by modifying the function in core file, line 279 :

<?php
        /**
         * Initializes all of the available roles.
         *
         * @since 4.9.0
         */
        public function init_roles() {
    if ( empty( $this->roles ) ) {
        return;
    }

    $this->role_objects = array();
    $this->role_names   = array();
    foreach ( array_keys( $this->roles ) as $role ) {
        // Vérifier si 'name' est défini pour le rôle actuel
        if ( isset( $this->roles[ $role ]['name'] ) ) {
            $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
            $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
        } else {
            // Gérer le cas où 'name' n'est pas défini (éventuellement définir une valeur par défaut)
            $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
            $this->role_names[ $role ]   = 'Nom par défaut';
        }
    }

    /**
     * Fires after the roles have been initialized, allowing plugins to add their own roles.
     *
     * @since 4.7.0
     *
     * @param WP_Roles $wp_roles A reference to the WP_Roles object.
     */
    do_action( 'wp_roles_init', $this );
}

Attachments (2)

notice-wp.jpg (93.9 KB) - added by mrsize 5 months ago.
screenshot of the notice
6144.diff (646 bytes) - added by jucaduca 5 months ago.

Download all attachments as: .zip

Change History (7)

@mrsize
5 months ago

screenshot of the notice

This ticket was mentioned in Slack in #core-test by ankit-k-gupta. View the logs.


5 months ago

This ticket was mentioned in PR #6144 on WordPress/wordpress-develop by @jucaduca.


5 months ago
#2

  • Keywords has-patch added; needs-patch removed

We have a problem when we register roles without 'name', as shown in the image, an error is generated.

https://github.com/WordPress/wordpress-develop/assets/252078/6ae6f2a8-ef91-4dab-b6ec-b30af1b53a77

I don't like the idea of defining a default name, but as tiket suggested, I followed the fix.

I will make other proposals below:

1: ignore roles without names

foreach ( array_keys( $this->roles ) as $role ) {
        if( ! isset( $this->roles[ $role ]['name'] ) ){
                continue;
        }

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

2: set default name at the beginning

foreach ( array_keys( $this->roles ) as $role ) {
        if( ! isset( $this->roles[ $role ]['name'] ) ){
                $this->roles[ $role ]['name'] = 'undefined'
        }

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

I don't have context on what the best way is, if anyone could help me I would be very grateful :bowtie:

Trac ticket: https://core.trac.wordpress.org/ticket/60579

@jucaduca
5 months ago

This ticket was mentioned in Slack in #core-test by krupajnanda. View the logs.


5 months ago

#4 @stenaus
2 months ago

Hi

Thanks for your input here! It helped me to resolve my issue.

As this is not bug (I guess), because no role should be without names, there are two options I guess.

1st option: install User Role Editor plugin and see what roles are not being used and delete them.
2nd option: Or go to the same file and location that you pointed out and dump all your roles array with

var_dump($this->roles);

just before foreach cycle.

And then see what unused roles you have. I had the same error with bbpress unused roles and some very old WordPress "Administrator" (note the capital A) role which is not used for centuries I guess.

For example I had to remove them manually, because bbpress plugin was not able to remove those roles automatically nor mentioned User Role Editor plugin was not able to remove Administrator role (I guess it matches with the administrator syntax, but had no time to debug this one further).

So, in my case I had to remove those roles manually:

Go to your theme's functions.php and remove those roles manually:

$wp_roles = new WP_Roles();
$wp_roles->remove_role("unused_role_goes_here");

After you have done refresh to your page you should not have unused roles anymore. Remove the PHP code from functions.php and enjoy your working WP.

HTH.

Last edited 2 months ago by stenaus (previous) (diff)

#5 @mrsize
3 weeks ago

Thanks @stenaus,

in addition, i have this function than can help to know whitch role has a name undefined :

function display_roles_admin_notice() {
    global $wp_roles;

    $roles_missing_names = [];
    foreach ($wp_roles->roles as $role_key => $role) {
        if (!isset($role['name'])) {
            $roles_missing_names[] = $role_key;
        }
    }

    if (!empty($roles_missing_names)) {
        $message = "Roles without names: " . implode(', ', $roles_missing_names);
        echo "<div class='notice notice-error'><p>{$message}</p></div>";
    }
}

add_action('admin_notices', 'display_roles_admin_notice');

i hope it can help

Note: See TracTickets for help on using tickets.