Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I target specific roles? #1

Open
rijotech opened this issue Jun 11, 2023 · 1 comment
Open

How can I target specific roles? #1

rijotech opened this issue Jun 11, 2023 · 1 comment

Comments

@rijotech
Copy link

Hi, I was wondering how can I load admin CSS on specific user roles only.
For example, I have a custom user role called LatePoint Agent and I only want to load the CSS to users associated with that role.

@coffee2code
Copy link
Owner

Hi @rijotech. There isn't direct support for limiting the custom CSS to certain users, but it's definitely something I'll consider adding in a future release. I'll leave this issue open as a reminder; I'll follow-up once it gets implemented or if I decide to pass on the idea.

In the meantime, you could achieve this in a fashion. The c2c_add_admin_css filter can be used to customize the CSS that gets output by the plugin in the admin. You could hook that and return an empty string if the current user has (or doesn't have) a given role.

For example:

// Don't output CSS from Add Admin CSS unless the current user
// has a particular role.
add_filter( 'c2c_add_admin_css', function( $css ) {
	if ( ! current_user_can( 'latepoint_agent' ) ) {
		$css = '';
	}
	return $css;
} );

If you're linking out to standalone CSS files, you'll have to do the same via the c2c_add_admin_css_files filter. (Otherwise this isn't necessary.)

// Don't link to CSS files from Add Admin CSS unless the current user
// has a particular role.
add_filter( '`c2c_add_admin_css_files`', function( $files ) {
	if ( ! current_user_can( 'latepoint_agent' ) ) {
		$files = [];
	}
	return $files;
} );

Obviously, customize the criteria to your particular situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants