0

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?

  • Is this your actual, exact code? Copied exactly into a fresh install does not cause any errors for me. Are you certain that the error being thrown is for this function? – Jacob Peattie Dec 19 '18 at 0:20
  • Hi Jacob, thanks for getting back to me - yes, that’s my exact code. I’m calling the function in the body tag of my header.php file, so will have a look at that and see if anything else could be triggering that error. Pretty sure it’s in this function though... – Osu Dec 19 '18 at 0:29
  • Wait, sorry, what do you mean "I’m calling the function in the body tag of my header.php file". Code for filters like this belongs in functions.php, and then you use <?php body_class(); ?> in your header file. – Jacob Peattie Dec 19 '18 at 0:32
  • Sorry, it was late when I posted that so I didn't make myself clear. This is what I have in my header: <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 Dec 19 '18 at 11:54
0

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');
0

This 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;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.