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' );
}
add_action()
that calls it?