2

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

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

Is any other step needed for importing script via WordPress?

5
  • You state you are using the method in the header, to enqueue scripts if must be before the wp_head() call. Unless of course, you want the script to load the script in the footer. Check out codex.wordpress.org/Function_Reference/wp_register_script
    – Cristian
    Apr 21 '11 at 16:01
  • Do i have to register script before enqueue? I thought some scripts are already registered by default in wordpress in wp-include/js folder
    – Sisir
    Apr 21 '11 at 16:14
  • Yes there are a load of already registered scripts : codex.wordpress.org/Function_Reference/… but if you want to use a custom script, then you will have to register it.
    – Cristian
    Apr 21 '11 at 16:27
  • It seems scripts are not registered in my case because wp_enqueue_script() is not working unless i register scripts again :(
    – Sisir
    Apr 21 '11 at 16:46
  • I am registering scripts by this code. I know this code is not perfect. Can you suggest more nifty code? wp_register_script( 'jquery', '/'. WPINC .'/js/jquery/jquery.js');
    – Sisir
    Apr 21 '11 at 16:48
6

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');
}
9
  • same result! Still scripts are not importing. But when i de-register then register the scripts again (with the path to the script in wp-include folder). Those are loading. What is the correct code for pointing the scripts in the wp-include directory?
    – Sisir
    Apr 21 '11 at 16:33
  • @Sisir if bundled jQuery doesn't work for you it's likely that theme or some plugin breaks it, sadly not uncommon.
    – Rarst
    Apr 21 '11 at 16:48
  • @Rarst : its not only jquery but all scripts :(
    – Sisir
    Apr 21 '11 at 16:50
  • 1
    @Sisir what does this get you - var_dump( wp_script_is( 'jquery', 'registered' ) ); ?
    – Rarst
    Apr 21 '11 at 16:54
  • @Rarst: returning: bool(true)
    – Sisir
    Apr 21 '11 at 17:39
3

If you have copy/pasted your code, then you have a typo.

 wp_enqueue_script('jquery');
1

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.

6
  • Yes! init action hook is the recommended place.
    – Ashfame
    Apr 21 '11 at 16:18
  • 1
    Codex suggests you use it with 'init' action hook. Actually it no longer does and that wasn't wildly correct either (too early for meaningful conditional queueing). wp_enqueue_scripts is most appropriate for front end.
    – Rarst
    Apr 21 '11 at 16:21
  • yes. used the init action hook. But still scripts are not importing. However when i de-register then register the scripts again. Those are loading. By the way, what is the correct code for pointing the scripts in the wp-include directory?
    – Sisir
    Apr 21 '11 at 16:29
  • You should not deregister and reregister them again. They are already registered. If they're not working, then you need to find the reason they're not working instead of trying a strange hack to work around it.
    – Otto
    Apr 22 '11 at 15:48
  • @Otto I know. Its just a work around until i find a solution. I know it may sound wired to you but i am working with wordpress about 6 months now and wp_enqueue_script never worked for me! Not even a single time. :(
    – Sisir
    Apr 23 '11 at 13:46
1

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

1
  • because wp_enqueue_script() is not including jquery. That's the whole reason of this post. I want to point the script to the right one that in wp-includes directory.
    – Sisir
    Apr 23 '11 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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