admin管理员组文章数量:1332889
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');
本文标签: Array to string conversion error in PHP 72 when returning user role as class
版权声明:本文标题:Array to string conversion error in PHP 7.2 when returning user role as class 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742315269a2451689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<?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