Load Custom Styles Conditionally Based on User Role

Last updated 1 month ago

PHP
function load_custom_styles_for_roles() {
    if ( current_user_can( 'administrator' ) ) {
        wp_enqueue_style( 'admin-styles', get_template_directory_uri() . '/css/admin-styles.css' );
    } elseif ( current_user_can( 'editor' ) ) {
        wp_enqueue_style( 'editor-styles', get_template_directory_uri() . '/css/editor-styles.css' );
    } else {
        wp_enqueue_style( 'default-styles', get_template_directory_uri() . '/css/default-styles.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'load_custom_styles_for_roles' );

All code snippets are licensed GPLv2 or later unless otherwise stated.