Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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' );
share|improve this answer
    
Thanks, that worked. But how about styles? There doesn't seem to be an "action" called `wp_enqueue_styles". –  its_me Jun 6 '12 at 18:07
3  
You use the same hook for both scripts and styles. See: codex.wordpress.org/Function_Reference/wp_enqueue_style –  mrwweb Jun 6 '12 at 18:08
    
@mrwweb Good call. Digital high-five! –  MathSmath Jun 6 '12 at 18:19

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;
.....
}
share|improve this answer
2  
Just to be clear, the only hooks that should be used with wp_enqueue_style() or wp_enqueue_script() are "wp_enqueue_scripts" or "admin_enqueue_scripts". If someone wants to enqueue a script on both the front and backend, they can use wp_register_script() (or style) once and then enqueue it with just the handle on both hooks. –  mrwweb Jun 6 '12 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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