0

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...

1

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");
});
  • great! It such a relief when the thing finally works! :p thanks ! – Paul May 15 '12 at 4:25

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.