1

I've been asked to restyle the css of a WordPress website frontend. The website can't be in maintenance mode while working on it as it must stay up and running all the time. I assume that if I can add a small and unobtrusive condition in the header allowing the administrator to see a different css file than the users, I could edit the css and test the result while the users still see the original version of the website. This way I could work on the design without anyone noticing.

Can anyone tell me which WordPress function I should call on the header to switch the css file if and only if the user is an administrator?

The best would be not to edit the "functions.php" file or activate any plugin as I really don't want to mess with the website as it is (few chances but chances anyway). I would rather edit the "header.php" file in the theme folder in order to be sure nothing bad happens.

Thank you.

1
  • Baylock, did my provided answer end up working for you? If so, click the check mark to the left of my answer. Feb 20, 2013 at 23:18

1 Answer 1

1

You should be able to simply check the capabilities of the currently logged in user. If they're an administrator the following example should do what you want it to. Just add this into your theme's functions.php file.

Please note this code is untested, but it should get you where you want to be. If the level_10 capability doesn't work you can check out the documentation for other user levels and capabilities here.

add_action( 'wp_enqueue_scripts', 'admin_only_stylesheet' );

function admin_only_stylesheet() {

    if ( current_user_can('level_10') {

        wp_register_style( 'admin-only-style', get_template_directory_uri() . '/css/admin-frontend-style.css', array(), '12345678', 'all' );

        wp_enqueue_style( 'admin-only-style' );

    }

}
4
  • Thank you. Like I said, I would rather modify the header.php file and create a condition on the css link. Would the "if" statement above work there? I don't have access yet to the website to verify.
    – Baylock
    Feb 20, 2013 at 0:20
  • 1
    Theoretically, yes the if statement should work just fine in this instance. I think I've used it outside of the context of a hook before and because it's a function that merely checks the current user global variable and checks against the info it has on that user, it should be fine. Feb 20, 2013 at 5:11
  • No worries Baylock, I'm glad I could help you out! Feb 21, 2013 at 2:33
  • I would suggest to use current_user_can( 'manage_options' ) since the level system is outdated.
    – Hexodus
    Feb 12, 2014 at 14:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.