最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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?

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
  • 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 Commented Dec 19, 2018 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 Commented Dec 19, 2018 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 Commented Dec 19, 2018 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 Commented Dec 19, 2018 at 11:54
Add a comment  | 

2 Answers 2

Reset to default 1

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

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)

  1. 暂无评论