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?
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?
Share Improve this question asked Dec 18, 2018 at 21:48 OsuOsu 1,4526 gold badges25 silver badges44 bronze badges 4 |2 Answers
Reset to default 1This code works and is perfectly valid, running on 7.3, except when your user has two roles:
add_filter( 'body_class', function( $classes = '' ) {
$current_user = new \WP_User(get_current_user_id());
$user_role = ['administrator', 'moderator']; //just a test, but this is what it'll look like if it had 2 roles.
$classes = [];
$classes[] = 'role-' . $user_role;
return $classes;
});
Then, what do you know, the same error appears. Also, you're passing a string as $classes
to your anonymous function, where-as the filter clearly demands an array.
Do this instead:
add_filter( 'body_class', function( $classes ) {
$current_user = wp_get_current_user();
foreach( $current_user->roles as $user_role ) {
$classes[] = 'role-' . $user_role;
}
return $classes;
});
I found that the problem was I was not checking if the user was logged in before adding the class. Here's my working code for anyone with the same issue:
header.php
<body <?php body_class( 'animate' ); ?> id="top">
(Note: the 'animate' class is irrelevant, it's just used in my site)
functions.php
function osu_add_role_to_body($classes = '') {
if( is_user_logged_in() ) {
$current_user = new WP_User(get_current_user_id());
$user_role = array_shift($current_user->roles);
$classes[] = 'role-' . $user_role;
}
return $classes;
}
add_filter('body_class','osu_add_role_to_body');
<?php body_class(); ?>
in your header file. – Jacob Peattie Commented Dec 19, 2018 at 0:32<body <?php body_class( 'animate'); ?> id="top">
so it's set up correctly. I seem to have found the problem - I needed to check the user is logged in before adding any classes as it was stripping all the classes from the body tag. Will post the answer now for anyone else stuck with the same issue - thanks for your help! – Osu Commented Dec 19, 2018 at 11:54