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.
  • I've been reading up a bit on this subject, but the more I read - the more confused I get.

  • Can someone explain to me in short what's the exact difference between wp_enqueue_scripts, wp_register_scripts and wp_print_scripts?

  • For example, I have the following code in my functions.php - and it's working, but I do not understand why I can not use wp_print_scripts for the stylesheets, whereas the code still works if I use wp_enqueue_scripts for the javascript files:

    add_action('wp_print_scripts', 'add_my_js');
    function add_my_js(){
        if(!is_admin()){
            wp_enqueue_script('default',  get_bloginfo('stylesheet_directory').'/js/default.js', array('jquery'));
        }
    }
    
    add_action('wp_enqueue_scripts', 'add_my_stylesheet');
    function add_my_stylesheet() {
        wp_register_style('default', get_bloginfo( 'stylesheet_url'));
        wp_enqueue_style( 'default');
    }
    
share|improve this question

1 Answer 1

up vote 7 down vote accepted

wp_print_scripts is the action that runs when scripts are output to the template. wp_register_script and wp_enqueue_script are functions for registering/enqueueing scripts to be output when wp_print_scripts runs.

you can't register or enqueue styles in the wp_print_scripts action hook because styles have already been output in the wp_print_styles hook, which runs before wp_print_scripts.

refer to the action reference to see the order things are executed in in a request:

22. wp_head
23. wp_enqueue_scripts
24. wp_print_styles
25. wp_print_scripts
share|improve this answer
    
So then, what is the difference between wp_enqueue_script and wp_register_script? –  Jeff K. Feb 29 '12 at 20:07
3  
wp_register_script registers the script with WordPress, but does not add it to the queue. If you don't also call wp_enqueue_script, the script will not appear. The purpose of this is to let you register your script once, then later conditionally enqueue it somewhere else in your code. This is useful for plugin developers where a script may be enqueued in multiple places within the code. You can also just enqueue your script directly without registering it. –  Milo Aug 22 '12 at 17:33

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.