How wp_enqueue_script works?

I am trying to get scripts via wp_enqueue_script();. I have tried this in header but WordPress is not importing any script.

I am using like this wp_enqueue_script('jquery');

Read More

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Is any other step needed for importing script via WordPress?

Related posts

Leave a Reply

4 comments

  1. In simple case you need to enqueue script before header scripts are printed, which happens in wp_head hook.

    Basic approach would be this in functions.php of your theme:

    add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
    
    function my_enqueue_scripts() {
    
        wp_enqueue_script('jquery');
    }
    
  2. Read what the codex says… You should call this inside an action hook… Otherwise it may lead to troubles! Codex suggests you use it with ‘init’ action hook.

  3. ^Listen to Otto Sisir!

    This is what I usually do for enqueueing custom js on the admin side, ONLY for my plugin’s settings page…

    `add_action('admin_print_scripts-settings_page_<your-settings-page-slug>', 'add_my_scripts');
    function add_my_scripts()
    {
       //We can include as many Javascript files as we want here.
       wp_enqueue_script('pluginscript', plugins_url('/js/script.js', __FILE__), array('jquery'));
    }
    

    `

    By the way, all you can do is use ‘wp_head’ hook and call wp_enqueue_script(‘jquery’) in the function, to use jquery on the frontend (theme) (same way you can include any Javascript library that is inside WP’s js folder). I don’t understand why and what you’re doing by this… wp_register_script( 'jquery', '/'. WPINC .'/js/jquery/jquery.js'); !