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.

my wp_enqueue_script does not seem to work, i can't understand why :

i have a child theme of twenty ten,
i wrote this in my functions.php file (in my theme) :

<?php
function my_scripts_method() {
    wp_enqueue_script(
        'patScript',
        get_stylesheet_directory_uri() . '/js/patScript.js',
        array('jquery')
    );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

then in my header :

<script type="text/javascript" src="<?php get_stylesheet_directory_uri().'/js/patScript.js'; ?>"></script>

and the file is :

$(function () {
    alert("hello");
});

But alert does not work. The path is correct, when i echo get_stylesheet... it gives me the right path where my file is located. Would you have any idea?

Thanks a lot


EDIT : the console in Firefox tells me that :

$(function () {

"is not a function" : does it mean it does not recognize jquery? i added :

wp_enqueue_script("jquery");

above my previous enqueue script, but same thing...

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Two things-

When you enqueue a script, WordPress inserts the script tag in the call to wp_head(), so step one is to remove the script tag from your header.

The second thing is the jQuery supplied with WordPress is in no conflict mode by default, so you should reference the jQuery object with jQuery rather than $:

jQuery(function () {
    alert("hello");
});
share|improve this answer
    
great! It such a relief when the thing finally works! :p thanks ! –  Paul May 15 '12 at 4:25
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.