1

I want to work on my style.css file. But It would be better if after all change and test I publish it for users. So Is there any way to make another style.css which load just for admin user?

1 Answer 1

2

You need to check the logged in user role before queuing it. So the code should be like bellow. you need to replace the plugins_url( 'my-plugin/css/plugin.css' ) with your stylesheet's path

function register_only_for_admin_styles() {
    if( current_user_can('editor') || current_user_can('administrator') ) { 
        wp_register_style( 'admin-style', plugins_url( 'my-plugin/css/plugin.css' ) );
        wp_enqueue_style( 'admin-style' );
    }
}
add_action( 'wp_enqueue_scripts', 'register_only_for_admin_styles' );

Try the code then let me know the result. Thanks

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, man. I edited it to ` wp_register_style( 'admin-style', get_template_directory_uri() . '/my-style.css', false, '1.0.0' );` and then it works.
but another problem appears! When I disable some CSS from my-admin-style, it comes back, because the main style loads too. @souvik-sikdar
Do you want to load the admin-style only for admins?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.