Including jQuery in WordPress

1) Only include scripts via wp_head(). For your custom script, register and enqueue it as such:

function mytheme_custom_scripts() {

    if ( ! is_admin() ) {
        $scriptsrc = get_stylesheet_directory_uri() . '/js/';
        wp_register_script( 'mytheme_custom', $scriptsrc . 'custom.js' );   
        wp_enqueue_script( 'mytheme_custom' );
    }

}
add_action( 'after_setup_theme', 'mytheme_custom_scripts' );

2) You need to deregister core jQuery, and then register and enqueue your minified version, like such:

function mytheme_minified_jquery() {

    if ( ! is_admin() ) {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' );  
        wp_enqueue_script( 'jquery' );
    }

}
add_action( 'init', 'mytheme_minified_jquery', 1000 );

Note that both include a if ( ! is_admin() ) conditional. You don’t want to mess with the scripts that load in the Admin UI.

Also, my example uses Google for the minified jQuery version. If you want to use your own bundled version, add the SRC to wp_register_script() instead.

Leave a Comment