edit role display name and label name without plugins

I’am creating WordPress function for editing role name

function change_role_name($role_name, $new_role_name, $display_name) {
    global $wp_roles;

    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

    $role =& get_role($role_name);

    $wp_roles->roles[$role]['name'] = $new_role_name; //not working for sa
    $wp_roles->role_names[$role] = $display_name;           
}

problem here it don’t change the role label name

Related posts

Leave a Reply

1 comment

  1. Those names are stored in the option wp_user_roles in the database table wp_options.

    So, the following will change the name of the subscriber role:

    $val = get_option( 'wp_user_roles' );
    $val['subscriber']['name'] = 'PeDeBoi';
    update_option( 'wp_user_roles', $val );
    

    Apparently, this is harmless, but caveat emptor

    In your code, $wp_roles->roles[$role]['name'] = $new_role_name; doesn’t work because it should be:

    $wp_roles->roles[$role_name]['name'] = $new_role_name;
    

    And $display_name doesn’t make much sense.