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 developing a theme and I have called jquery along with other JS files from a theme-enqueue.php file. For some reason only the Jquery file that is hosted on googles servers is adding the theme URL into the URL path so it is not loading it and causing other JS files to not load properly.

The hook is done properly and it is pointed only to their server and does not include a get_bloginfo or get_template_uri.

The file it is adding my theme URL to is:

    'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', '', '1.4.2', true);

Here is the full function within my enqueue file. Can anyone see any issues?

function init_scripts() {
wp_deregister_script( 'jquery' );
wp_deregister_script( 'comment-reply' );
// Register Scripts
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
wp_register_script( 'comment-reply', get_bloginfo('url') . '/wp-includes/js/comment-reply.js?ver=20090102');
// Queue Scripts
wp_enqueue_script('modernizr', get_bloginfo('template_url') . '/js/modernizr-1.5.min.js', '', 1.5, false);
wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', '', '1.4.2', true);

if ( get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply',  get_bloginfo('url') . '/wp-includes/js/comment-reply.js?ver=20090102', 'jquery', '', true );
}
share|improve this question
3  
Always use WordPress’ built-in jQuery, or you will break all other plugins and core functionality. –  toscho Apr 17 '13 at 17:48
    
Always load the same jQuery as used by the WordPress Core, at least. Why are you loading such an old jQuery? jQuery is up to 1.9.1 and WordPress 3.5.1 is using jQuery 1.8.3. –  s_ha_dum Apr 17 '13 at 18:04
    
Can we please see the full callback, as well as the add_action() that calls it? –  Chip Bennett Apr 17 '13 at 19:53
add comment

2 Answers

up vote 2 down vote accepted

If you register a script with wp_register_script() it is not neccessary to pass all the options in wp_enqueue_script() again.

You can register the script in a central place and use the enqueueing it in diffenrent files.

functions.php
wp_register_script( 'theme_script', 'path/to/script.js', array( [dependencies] ), '1.0', true );

[...]

if ( $condition )
  require_once 'file1.php';
else
  require_once 'file2.php';

file1.php
wp_enqueue_script( 'theme_script' );

file2.php
wp_enqueue_script( 'theme_script' );

Adding a source to wp_enqueue_script() after registering it earlier will lead to some trouble. Let's have a look into the core file ( wp-includes/functions.wp-scripts.php )

if ( $src ) {
  $_handle = explode('?', $handle);
  $wp_scripts->add( $_handle[0], $src, $deps, $ver );

What does this mean for your theme?

At first you register a script with the handle "jquery" and the source "http://ajax.googleapis.com/ajax...". Than you enqueue a script with the handle "jquery" and the source "http://ajax.googleapis.com/ajax...".

Look at the core code: if ( $src ) means, if there is a source passed with wp_enqueue_script(), apply the first part of the source (before the ? ) to the handle and register a script with this handle and this source.

So you registered a script with the handle "jquery" and the source "http://ajax.googleapis.com/ajax..." with wp_register_script(). And you registered and enqueued a script with the handle "http://ajax.googleapis.com/ajax..." and the source "http://ajax.googleapis.com/ajax...". Later you try to enqueue a script (comment-reply) that depends on "jquery", but the jquery script is only registered but not enqueued.

This leads to unexpected behavior.

Dependencies should be passed as array (array( 'jquery' )) (see Codex here and here). And in development turn WP_DEBUG on and check if the files exists. There is really no need to deregister a core script and re-registering the same file again. If you need a unminified version (e.g. for debugging), use the SCRIPT_DEBUG constant.

Your script should look like this:

function init_scripts() {
wp_deregister_script( 'jquery' );

// Register Scripts
if ( ! defined( 'SCRIPT_DEBUG' ) && ( defined( 'WP_DEBUG' ) && true == WP_DEBUG ) )
 define( 'SCRIPT_DEBUG', true );

$modernizr_file = get_bloginfo('template_url') . '/js/modernizr-1.5.min.js';
if ( defined( 'WP_DEBUG' ) && true == WP_DEBUG && ! file_exists( $modernizr_file ) )
  throw new Exception( 'File not found for JS modernizr:' . $modernizr_file );

wp_register_script( 'modernizr', $modernizr_file, '', 1.5, false );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', '', '1.4.2', true );

// Enqueue scripts, dependencies first
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'modernizr' );

if ( get_option( 'thread_comments' ) )
  wp_enqueue_script( 'comment-reply' );

}
share|improve this answer
    
Thank you for taking the time, your post helped me solve the issue and I actually learned some stuff along the way. –  user1632018 Apr 19 '13 at 5:03
add comment

Your jQuery dependency should be wrapped in an array.

wp_enqueue_script( 'comment-reply',  get_bloginfo('url') . '/wp-includes/js/comment-reply.js?ver=20090102', 'jquery', '', true );

should be

wp_enqueue_script( 'comment-reply', get_bloginfo('url') . '/wp-includes/js/comment-reply.js', array( 'jquery' ), '20090102', true );

If that is still not working try just adding wp_enqueue_script( 'jquery' ); before hand.

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.