2

I always, always have trouble getting jQuery to work in wordpress.

I really need some clarification and explination on just how this thing works.

Here is my code and I can't seem to see what is wrong with it.

In the functions.php file:

function my_init() {
if (!is_admin()) {
    // comment out the next two lines to load the local copy of jQuery
    // wp_deregister_script('jquery'); 
    // wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2'); 
    wp_enqueue_script('jquery');
} } add_action('init', 'my_init');

And this is in my footer before I call my other jQuery scripts:

<?php wp_enqueue_script("jquery"); ?>
3
  • I have this problem also.I mean i load it somehow but i always get trouble's with wp_enqueue_script. Commented Oct 29, 2012 at 12:30
  • Can you describe what kind of problems you are having with this? Commented Oct 29, 2012 at 12:43
  • Yes jQuery isn't loading. I have removed this and added a script tag into the document and it loads fine. But with just this... doesn't load. Commented Oct 29, 2012 at 12:44

1 Answer 1

1

There're some answers in this post also.

I've encountered this also in the past, and I usually don't even use the wordpress own jquery if needed. Because the serverside google method is faster. This code below has to work.

functions.php

<?php

function google_jquery() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
} 

add_action('wp_enqueue_scripts', 'google_jquery');

?>

Make sure that wp_head(); is in your header.php file.

header.php

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title(); ?> <?php bloginfo( 'name' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
<?php if ( is_singular() && get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' ); ?>
<?php wp_head(); ?>
</head>

You don't need to call the other jquery scripts in the footer that way. If you're enqueueing lets say jquery UI scripts then make sure you have wp_footer(); in your footer.php file.

footer.php

<?php 
//Footer scripts
wp_footer(); 
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.