Enqueued scripts and styles loading in WordPress Dashboard as well

For a moment, I wondered if my test site got hacked — my WordPress site’s dashboard was all broken (cosmetically). But when i looked at the source code of the page, I noticed that the “scripts and styles” that I enqueued for the front-end were also enqueued in the backend as well.

This is how I did it:

Read More
add_action('init','wpse54189_register_script');
function wpse54189_register_script(){

    wp_register_script( 'aahan_bootstrap_transition', get_template_directory_uri().'/js/bootstrap-transition.js');
    wp_register_script( 'aahan_bootstrap_carousel', get_template_directory_uri().'/js/bootstrap-carousel.js');
    wp_register_script( 'aahan_bootstrap_carousel_cycler', get_template_directory_uri().'/js/bootstrap-carousel-cycler.js', array('jquery', 'aahan_bootstrap_transition', 'aahan_bootstrap_carousel'));
    wp_enqueue_script( 'aahan_bootstrap_carousel_cycler' );

    wp_enqueue_script( 'comment-reply' );

    wp_register_script( 'aahan_ajax_comment', get_template_directory_uri().'/js/no-reload-comments.js', array('jquery'));
    wp_localize_script( 'aahan_ajax_comment', 'yjlSettings', array(
         'gifUrl'=> get_template_directory_uri().'/images/ajax-loader.gif',
         'autoGrow'=> 'enable'
    ));
    wp_enqueue_script( 'aahan_ajax_comment' );

}

add_action('init','aahan_register_style');
function aahan_register_style(){
    wp_register_style( 'aahan_webfonts_stylesheet', get_template_directory_uri().'/font/font.css');
    wp_register_style( 'aahan_main_stylesheet', get_template_directory_uri().'/style.css', array('aahan_webfonts_stylesheet'));
    wp_enqueue_style( 'aahan_main_stylesheet' );
}

What could be wrong here?

Related posts

Leave a Reply

2 comments

  1. The “init” action runs both on frontend page loads, and on backend page loads.

    Try hooking these to the “wp_enqueue_scripts” action instead. I believe it does not run on admin page loads.

    Example Code: (By OP)

    function wpse54388_scripts_styles() {
    
        wp_enqueue_style( ... );
    
        wp_enqueue_script( ... );
    
    }
    add_action( 'wp_enqueue_scripts', 'wpse54388_scripts_styles' );
    
  2. Just add the condition

    if( is_admin() ) return;
    

    So you will have

    add_action('init','wpse54189_register_script');
    function wpse54189_register_script(){
        if( is_admin() ) return;
    .....
    }
    
    add_action('init','aahan_register_style');
    function aahan_register_style(){
    
        if( is_admin() ) return;
    .....
    }