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 am trying to use a self written script for the jQuery library in my self-made theme, but it's not working. When I simply use script-tags, everything is alright. So it has to be something with wp_enqueue_script and wp_register_script. In the beginning of my code in the index.php I have

<?php
    wp_enqueue_script('jquery');
    wp_register_script( 'my_name', 'path/to/my/script/script.js'));
    wp_enqueue_script('my_name');
?>

I know that normally that has to be written BEFORE wp_head(); but because I am still developing everything, I haven't split my code into various files, I am working straight in the index.php. Do I have to do any other steps before enqueueing the scripts?

share|improve this question
add comment

3 Answers 3

You want to use wp_enqueue_scripts.

add_action( 'wp_enqueue_scripts', 'wpse_3810' );
function wpse_3810() {
    wp_enqueue_script( 'jquery' );
    wp_register_script( 'my_name', 'path/to/my/script/script.js'));
    wp_enqueue_script( 'my_name' );
}
share|improve this answer
1  
Yes, but technically, no earlier than init. Using wp_enqueue_scripts is probably more semantically correct. –  Chip Bennett Jan 6 '12 at 16:21
3  
Doing enqueue on init is wrong and myth, long propagated by incorrect info in Codex. wp_enqueue_scripts is correct hook to use for that. –  Rarst Jan 6 '12 at 17:12
    
Oh, good to know. I've changed the code appropriately. –  v0idless Jan 6 '12 at 17:24
add comment

There is a 'wp_enqueue_scripts' hook for this, 'init' is usually not the right choice.

share|improve this answer
add comment

I would recommend you put your code in functions.php as that would be loaded before your index.php

You should also define dependencies so that they are loaded in the correct order.

<?php
//wp_register_script( $handle, $src, $deps, $ver, $in_footer );
wp_register_script( 'my_name', 'path/to/my/script/script.js', array('jquery') );
?>

See: wp register script

share|improve this answer
add comment

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.