0

I need help with something for my wordpress site. I want to add custom css to a specific user. For example, user B. but I couldn't do it.

Here I tried the following css code:

div [user="id"] { color: #ffff;}

How can I add custom css to specific user? I would be glad if you help. Thanks

1 Answer 1

0

In the code that you proposes the selector only works if previously added the attributes in the HTML. It is easier if you load a specific stylesheet just for certain users:

Create a new CSS file with the specific styles, something like specific.css

.just-for-user-b {
  color: #ffffff;
}

Add this code to your functions.php

<?php
/**
 * Load CSS only for specific logged-in users.
 */
function load_specific_css() {
    $user = wp_get_current_user();
    if ( $user ) {
        $auth_users = array( "B" ); // You can add users.
        if ( in_array( $user->user_login, $auth_users ) ) {
            wp_enqueue_style( 'specific-styles', get_stylesheet_directory_uri() . '/specific.css' );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'load_specific_css', 10 );

By this way you keep the specific styles separated and easy to maintain, the CSS file only will be loaded by the specific logged in users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.