Why are my styles being applied to the admin area?

All my enqueued styles are applied to the back-end as well. I have never encountered this behavior before.

Here’s my code:

wp_register_style( 'main-styles', get_stylesheet_directory_uri() . '/css/styles.css', false, '1.0.0', 'all');

wp_enqueue_style('main-styles');

2 Answers
2

Why are my styles being applied to the admin area?

Assuming that is all of the code, it is because you are enqueueing the styles globally. You need to hook that to wp_enqueue_scripts, which will only load on the front end.

wp_register_style( 'main-styles', get_stylesheet_directory_uri() . '/css/styles.css', false, '1.0.0', 'all');

function enqueue_script_front_wpse_84975() {
  wp_enqueue_style('main-styles');
}
add_action('wp_enqueue_scripts', 'enqueue_script_front_wpse_84975');

You can register globally or put that wp_register_scripts part inside the enqueue_script_front_wpse_84975 function, but you need to hook wp_enqueue_style, at least, to wp_enqueue_scripts or you will enqueue the stylesheet everywhere. I realize it seems weird to enqueue a stylesheet on a hook ending in “script” but that is correct per the Codex.

wp_enqueue_style can be, and is, used to enqueue anywhere, front or back end, as you can see on the Codex page for admin_enqueue_scripts

Leave a Comment