0

I'm trying load a style sheet is a user role is subscriber, what am I doing wrong:

<?php
global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "subscriber" ) ){ 
echo '<link rel="stylesheet" href="/login/subscriber-style.css">'
} 
?>

This is in the header.php of the theme

Ben Jones
  • 504
  • 4
  • 18

1 Answers1

2

You're using an outdated method of getting the current user role. get_currentuserinfo() was deprecated in WordPress 4.5.

https://codex.wordpress.org/Function_Reference/get_currentuserinfo

Also, you shouldn't be loading CSS directly from your header.php file, you need to enqueue it instead.

Add the following code to your functions.php file.

function wpse_load_subscriber_stylesheet() {
    if ( current_user_can( 'subscriber' ) ) {
        wp_enqueue_style( 'login-subscriber-style', home_url( '/login/subscriber-style.css' ) );
    } 
}
add_action( 'wp_enqueue_scripts', 'wpse_load_subscriber_stylesheet' );

This assumes that your CSS is placed in your web root (https://example.com/login/subscriber-style.css). If it's in your theme folder instead then you need get_template_directory_uri() . '/login/subscriber-style.css'.

https://developer.wordpress.org/reference/functions/wp_enqueue_style/

Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58