• Is there a way to do this? To detect the user who is logged in, and pass his user name into a class attached to the body, like so:

    <body class="logged-in logged-in-as-Who_Dat">

Viewing 2 replies - 1 through 2 (of 2 total)
  • You could use wp_get_current_user() for this purpose:

    function add_user_nicename( $classes ) {
      if ( is_user_logged_in() && wp_get_current_user() ) :
        $current_user = wp_get_current_user();
        $current_user_nicename = $current_user->user_nicename;
        $classes[] = 'logged-in-as-' . $current_user_nicename;
      endif;
    
      return $classes;
    }
    add_filter( 'body_class', 'add_user_nicename' );
    Thread Starter Who_Dat

    (@who_dat)

    Thanks. I had a low-tech way of doing this:

    <?php
    $user_id = get_current_user_id();
    if ($user_id == 0) {
    	echo '<body>';
    } else {
    	echo '<body class="logged-in user_'.$user_id.'">';
    }
    ?>

    …and it works fine. It just doesn’t allow for other classes to be added that might be useful in their own right.

    [BTW…for some reason I didn’t get an email notification on this. Sorry to be so late acknowledging.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Body class "logged-in-as-[user name]"’ is closed to new replies.