Array to string conversion error in PHP 7.2 when returning user role as class

Question

Disclaimer: I’m rubbish at PHP, so please bear with me as I’m still learning. I’m getting the PHP error Array to string conversion for the following code:

function osu_add_role_to_body($classes = '') {
    $current_user   = new WP_User(get_current_user_id());
    $user_role      = array_shift($current_user->roles);
    $classes        = [];
    $classes[]      = 'role-' . $user_role;
    return $classes;
}
add_filter('body_class','osu_add_role_to_body');

This only started happening since I upgraded to PHP 7.2 so I’m assuming things have changed with how I need to deal with arrays right? Any idea of how to fix?

0
, , Osu 4 years 2020-02-22T08:38:36-05:00 0 Answers 53 views 0

Leave an answer

Browse
Browse