{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
add body class for logged in users
The robot generated the following code:
Here is the WordPress code snippet to add a body class for logged in users, prefixing functions with "nm_":
“`php
function nm_add_body_class_for_logged_in_users($classes) {
if ( is_user_logged_in() ) {
$classes[] = 'logged-in';
}
return $classes;
}
add_filter('body_class', 'nm_add_body_class_for_logged_in_users');
“`
In this snippet:
– We're defining a function called `nm_add_body_class_for_logged_in_users`. This function takes one parameter, `$classes`, which is an array of all the classes that will be added to the body tag.
– Inside the function, we are checking if the user is logged in using the `is_user_logged_in()` function. If the user is logged in, we add the 'logged-in' class to the `$classes` array.
– We attach our function to the `body_class` filter using `add_filter()`. This filter is applied to the list of body class names before they are given to the body tag. Our function will alter this list by adding our new class if the user is logged in.
– In the end, the function returns the modified `$classes` array.