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
- 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
- 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.”?
- Enable errors PHP WordPress 5.2
- 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
- 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]
- Ajax return code 400
- Add user settings to specific roles
- 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
- Error on wp_default_style After upgrade to php 7
- custom user role wordpress – grant guest access to edit.php without insert/update/delete
- Error: options page not found
- Which is the correct way to conditionally enqueue a CSS file?
- “Undefined index” error when saving empty array with checkboxes
- Preg_Match(): compilation failed unknown property name
- customize wordpress database error page
- How to keep the capability of users and disable Gutenberg editor in WordPress?
- Errors after upgrading PHP to 7.4 WordPress
- How to end a while loop when used in a fetch assoc function [closed]
- After adding my website to a new server, I keep getting a unexpected end of file error, but the file is identitcal to it’s original source
- Shortcode for Listing Users from Meta Value?
- javascript variable to wordpress php variable
- PHP E_WARNING being shown despite php.ini [closed]
- Query for user roles
- Angular not defined [closed]
- How can I call a specific file (via php) by referencing the logged-in username?
- Help with accessing wp-admin page and resolving error messages
- Blog suddenly shows Archive instead of index page
- 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
- Illegal string offset in PHP function in via Theme Customizer
- How to Add a cutsom slug to my custom author role
- Allow a user or role to view drafts and previews, but not other admin privileges?
- Cannot find the php_ini configuration file to find my error log to see why my code is not working
- Fatal error login WordPress [duplicate]
- I am having errors with checkout on wordpress
- Intermittent 503 Service Unavailable Error
- how to create a user rule that can only manage support tickets?
- Trying to get property ‘ID’ and ‘post_author’ of non-object error
- HELP: cannot log into wordpress. PHP error
- PHP error on local server install
- WordPress files break if I edit them, but adding a closing PHP tag fixes it
- PHP Error in WordPress Theme
- WordPress database error only while loading page for 1st time
- This occurs when I open admin or front on localhost
- Add another role to a user when they click a button?
- Obtain wordpress user role
- Is there a way to search for a missing endif?
- How to properly create multiple conditions to redirect users roles to different pages
- style.min.css code issue
- PHP is_embed error showing up
- WordPress 3.0 PHP Error – notice WordPress_product_Type was called incorrectly
- 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.
- Too few arguments for printf() [closed]
- syntax error unexpected ‘}’ at 364 line
- Troubleshooting “loopback requests that take too long”
- Data inserted in database, but ajax calls error function
- Parse error: syntax error, unexpected ‘}’ in C:\wamp64\www\Proiect\aplicatie\user_check.php on line 18 [closed]
- What does that mean: cannot use a scalar value as an array [closed]
- problem with php function error
- 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]
- PHP warning $post->ID is null
- How to lock users account until approvation
- 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
- How to add custom user role into wordpress