You can use the body_class
filter and the wp_get_current_user()
function to add the current user's role to the <body>
element, and then hide your elements with that (untested):
add_filter( 'body_class', static function ( $classes ) {
$user = wp_get_current_user();
$roles = $user->roles;
if ( ! is_array( $roles ) ) {
$roles = array( 'subscriber' );
}
foreach ( $roles as $role ) {
$classes[] = sprintf( 'role-%s', $role );
}
return $classes;
} );
body.role-subscriber #element-id,
body:not( .role-administrator ) .admins-only {
display: none;
}
Please note that hiding sensitive information using CSS is not effective, as the information will be available in the page's source code: it is much more secure to use PHP to check the user's role and include the sensitive information only in that case.