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;
});
Related Posts:
- Remove update nags for non-admins [duplicate]
- Allow Editors to edit pending posts but not draft ones
- PHP Catchable fatal error: Object of class WP_Error could not be converted to string
- WSOD but WP_DEBUG not giving any errors
- WordPress “Link has expired” error on updating posts
- get_the_content if it contains multiple lines it results in SyntaxError
- How to resolve error “Cookies are blocked due to unexpected output.”?
- error_log is not working as expected in functions.php file
- Enable errors PHP WordPress 5.2
- “Can’t use function return value in write context” error
- “Notice: Undefined variable: content” is showing [closed]
- Does WordPress have a built in reference to the PHP version its running under? [closed]
- Can WordPress email the admin about PHP errors, while hiding them from the site?
- Display user role Name – not slug
- White Screen of Death – wp-admin
- Ajax return code 400
- Add user settings to specific roles
- WordPress 5 WP REST routes – No errors
- Hiding div based on user role
- WordPress Script stops suddenly
- A non-numeric value encountered in /wp-includes/functions.php on line 68
- Do not execute If User is login as Administrator or any Specific Role
- Getting error when using wp_insert_post()
- Headers already sent in Pluggable.php / homepage and wp-login error
- “Undefined index” error when saving empty array with checkboxes
- Add post with approval by the admin
- Warning: Illegal string offset on theme options page [closed]
- PHP if statement works in template files, but not header
- Errors after upgrading PHP to 7.4 WordPress
- PHP-warning in post-template.php and no loading content on pages
- Parse error: syntax error, unexpected ‘}’ [closed]
- PHP E_WARNING being shown despite php.ini [closed]
- What’s wrong with this piece of code? [closed]
- Noindex subscriber author page
- Query for user roles
- can’t get errors
- Cannot load media and I get “PHP Warning: Invalid argument supplied for foreach(“
- Fatal Error wp-settings.php on line 119
- How can I fix this code [duplicate]
- Trying to display terms from custom taxonomy within function
- Plugin: Front-end Editor – User role specific editable content?
- Too many errors on log
- Programmatically revert to backup .htaccess a possibility?
- Prevent post from being published and show error
- Execute PHP code only with specific user role
- Edit and delete permissions pages and posts
- Execute function only for specific user roles
- Can’t log in to WordPress wp-admin after adding code to functions.php
- White screen after migrating site, but wp-admin works fine. Disabling all plugins, applying default theme and enabling debug mode does nothing
- Fatal error login WordPress [duplicate]
- Hide payment method based on user role AND minimum cart value
- PHP Warning: mysqli_query(): after updating my websites php from 5.6 to 7.2
- Edit a function to take different actions based on user role
- WordPress Bad Request Error 400
- HELP: cannot log into wordpress. PHP error
- Why am I getting syntax error, unexpected ‘endwhile’ (T_ENDWHILE) on single.php
- Used a code to revoke some dashboard menus for my contributors but for some reason it’s revoked me access to the editor [closed]
- The link you followed has expired. Please try again
- PHP error on local server install
- How to give custom roles the capability to edit one Menu instead of every Menu
- Site title not showing. Please help me
- Getting an empty space in up sell items
- This occurs when I open admin or front on localhost
- How to properly create multiple conditions to redirect users roles to different pages
- Echo Option Value Based On WordPress User Role
- style.min.css code issue
- How to send logs to plugin owner for a plugin?
- Role exception for a settings sub menu
- global $post inside plugin query messes up every new post page in wp-admin
- Allow BBPress participant role to trash topics
- Headers Already Sent
- Functions are causing errors
- Get author meta of all writers
- Too few arguments for printf() [closed]
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- WordPress won’t display errors at all
- Troubleshooting “loopback requests that take too long”
- Switch-Case and user_role
- Parse error: syntax error, unexpected ‘}’ in C:\wamp64\www\Proiect\aplicatie\user_check.php on line 18 [closed]
- Query Concatenation
- WP Debug enabled Undefined index error in a widget
- php 7.0 update to 7.1 and at least 7.2
- How to handle PHP parse errors?
- Forbid certain users to access a specific page
- WordPress White Screen Error
- Need help with Deprecated: Non-static error when update PHP 7.4 -> 8.1 with Dyad 2 theme
- kali php problem [closed]
- PHP warning: Undefined array key 2 in feed.php
- Execute multiple PHP Snippets causes error?
- Use of undefined constant issue
- How to create custom user role without plugin?
- WordPress 6.1.1 shows deprecated errors with PHP 8.1 & 8.2
- Custom Admin Menu Report for Specific User ID
- I can’t log into my website , it says “Error: Cookies are blocked due to unexpected output”
- WordPress @include( ‘template-config.php’ );
- “Unexpected response from server” error when trying to upload any .mp4 file over 100MB
- taxonomy pages returning “NULL” when running default WordPress function ‘get_queried_object()’
- WordPress Create New Role Just to Manage Users?
- Undefined constant ABSPATH
- Hide ID for WordPress User Role Subscriber