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

Related posts

Leave a Reply

2 comments

  1. 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

  2. Where are you hooking these?

    If you look at the codex, you should add these to a function and hook the function to wp_enqueue_scripts for front end display.

    Example:

    function add_my_css(){
    wp_register_style( 'main-styles', get_stylesheet_directory_uri() . '/css/styles.css', false, '1.0.0', 'all');
    
    wp_enqueue_style('main-styles');
    }
    
    add_action('wp_enqueue_scripts', 'add_my_css');