Skip to Content
Frontlane Studio
All Snippets
PHP WordPress November 14, 2024

Load Custom Styles Conditionally Based on User Role

A PHP snippet for Load Custom Styles Conditionally Based on User Role.

WordPress PHP

A PHP snippet for Load Custom Styles Conditionally Based on User Role.

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' );
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' );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' );

/**
 * Enqueues a custom stylesheet based on the user's role.
 *
 * This function checks the current user's role and loads the appropriate CSS file.
 * It's designed to be easily scalable by adding new roles and stylesheet mappings
 * to the $role_styles array. A default stylesheet is loaded for guests or users
 * without a specific stylesheet.
 */
function load_custom_styles_for_roles_improved() {
	// A mapping of user roles to their specific stylesheets.
	// The order of this array is important: the first role that matches for a user is the one that will be used.
	// This gives administrators priority over editors, etc.
	$role_styles = [
		'administrator' => 'admin-styles.css',
		'editor'        => 'editor-styles.css',
		// Add more roles here, e.g., 'author' => 'author-styles.css'
	];

	$style_to_load = 'default-styles.css'; // The default stylesheet for guests and other roles.
	$user          = wp_get_current_user();

	// Check if the user is logged in and has roles.
	if ( $user instanceof WP_User && ! empty( $user->roles ) ) {
		// Find the correct stylesheet by checking the user's roles against our mapping.
		foreach ( $role_styles as $role => $style_file ) {
			if ( in_array( $role, $user->roles, true ) ) {
				$style_to_load = $style_file;
				break; // Stop at the first match to respect the priority.
			}
		}
	}

	// Generate a file path to be used for cache busting.
	$style_path = get_template_directory() . '/css/' . $style_to_load;

	// Ensure the file actually exists before trying to enqueue it.
	if ( file_exists( $style_path ) ) {
		wp_enqueue_style(
			'custom-role-styles', // A single, consistent handle for the stylesheet.
			get_template_directory_uri() . '/css/' . $style_to_load,
			[], // No dependencies.
			filemtime( $style_path ) // Cache busting: appends the file's last modification time as the version.
		);
	}
}
add_action( 'wp_enqueue_scripts', 'load_custom_styles_for_roles_improved' );
/**
 * Enqueues a custom stylesheet based on the user's role.
 *
 * This function checks the current user's role and loads the appropriate CSS file.
 * It's designed to be easily scalable by adding new roles and stylesheet mappings
 * to the $role_styles array. A default stylesheet is loaded for guests or users
 * without a specific stylesheet.
 */
function load_custom_styles_for_roles_improved() {
	// A mapping of user roles to their specific stylesheets.
	// The order of this array is important: the first role that matches for a user is the one that will be used.
	// This gives administrators priority over editors, etc.
	$role_styles = [
		'administrator' => 'admin-styles.css',
		'editor'        => 'editor-styles.css',
		// Add more roles here, e.g., 'author' => 'author-styles.css'
	];

	$style_to_load = 'default-styles.css'; // The default stylesheet for guests and other roles.
	$user          = wp_get_current_user();

	// Check if the user is logged in and has roles.
	if ( $user instanceof WP_User && ! empty( $user->roles ) ) {
		// Find the correct stylesheet by checking the user's roles against our mapping.
		foreach ( $role_styles as $role => $style_file ) {
			if ( in_array( $role, $user->roles, true ) ) {
				$style_to_load = $style_file;
				break; // Stop at the first match to respect the priority.
			}
		}
	}

	// Generate a file path to be used for cache busting.
	$style_path = get_template_directory() . '/css/' . $style_to_load;

	// Ensure the file actually exists before trying to enqueue it.
	if ( file_exists( $style_path ) ) {
		wp_enqueue_style(
			'custom-role-styles', // A single, consistent handle for the stylesheet.
			get_template_directory_uri() . '/css/' . $style_to_load,
			[], // No dependencies.
			filemtime( $style_path ) // Cache busting: appends the file's last modification time as the version.
		);
	}
}
add_action( 'wp_enqueue_scripts', 'load_custom_styles_for_roles_improved' );