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]
- Should I use spl_autoload_register() in my plugin?
- WP-CLI – Selecting PHP version
- error_log() output for print_r() appearing on page
- When to use Exceptions vs Error Objects vs just plain false/null
- Allow Editors to edit pending posts but not draft ones
- wp_insert_user role not working
- Hide php Notices in Dashboard
- PHP Catchable fatal error: Object of class WP_Error could not be converted to string
- Upgrading PHP version results in “Use of undefined constant WP_CONTENT_DIR” warning?
- WSOD but WP_DEBUG not giving any errors
- How to target with css, admin elements according to user role level?
- Remove admin bar for subscribers
- WordPress “Link has expired” error on updating posts
- Editor role cannot save custom theme options
- wordpress separate registration for different roles
- get_the_content if it contains multiple lines it results in SyntaxError
- Notice: Constant already defined in wp-config.php on (non-existent) line?
- 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
- Fatal error: Call to undefined function wpsc_cart_item_count()
- Why when I instantiate wp_error in a validation method my user registration method stops working?
- Error when requesting password reset email – wp authentication
- “Notice: Undefined variable: content” is showing [closed]
- After upgrading to PHP 7.0 my contact form outputs error
- 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
- How to give new users two specific user role options upon WordPress user registration
- How to find error in my code when the error message is pointing to WP core file?
- get_term_link() returns correct – But illogical error-messages disturb
- PHP Warning: Attempt to read property “term_id” on bool
- Hiding div based on user role
- Mute Debug Messages from Plugins
- WordPress Script stops suddenly
- A non-numeric value encountered in /wp-includes/functions.php on line 68
- Error on wp_default_style After upgrade to php 7
- custom user role wordpress – grant guest access to edit.php without insert/update/delete
- How to send logs to plugin owner for a plugin?
- WordPress 3.0 PHP Error – notice WordPress_product_Type was called incorrectly
- 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
- Custom user roles doesn’t apply changes
- Headers Already Sent
- Include administrator in author list
- How to connect database table to each registered wordpress user.
- Functions are causing errors
- Get author meta of all writers
- Too few arguments for printf() [closed]
- syntax error unexpected ‘}’ at 364 line
- 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”
- Data inserted in database, but ajax calls error function
- 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
- What does that mean: cannot use a scalar value as an array [closed]
- problem with php function error
- How to handle PHP parse errors?
- Forbid certain users to access a specific page
- PHP warning Invalid argument supplied for foreach() on custom page template
- WordPress White Screen Error
- Please help! Parse error: syntax error, unexpected ‘
- Not able to see the error [closed]
- 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
- PHP warning $post->ID is null
- How to lock users account until approvation
- Getting invalid user ID error when creating a new user with wp_insert_user
- Execute multiple PHP Snippets causes error?
- Use of undefined constant issue
- How to create a User Role and give permission to only use Web Stories plugin?
- 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”
- Error when loading Dashboard
- How to add custom user role into wordpress
- 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
- PHP Warning: Undefined array key “HTTP_REFERER”
- PHP Warning: Undefined array key “VirtualHostSubMenu” in index.php
- Headers already sent error
- Warning: Undefined variable $post_id
- Hide ID for WordPress User Role Subscriber
- Facing 503 error on PHP files after deleting hidden files from the root folder