Return to Snippet

Revision: 47767
at June 16, 2011 04:19 by jschultz76


Initial Code
<?php

function my_script_init() {
	if (!is_admin()) {
		// deregister local copy of jQuery
		wp_deregister_script('jquery');
		// register google hosted jquery
		wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', false, '1.6.1');
		wp_enqueue_script('jquery');

		// register some other javascript file (just for the sake example)		
		wp_register_script('otherfile', get_bloginfo('stylesheet_directory').'/scripts/jquery.otherFile.js', array('jquery'));
		wp_enqueue_script('otherfile');

		// register another file that requires that the other two files are loaded first		
		wp_register_script('mainjs', get_bloginfo('stylesheet_directory').'/scripts/main.js', array('jquery','otherfile'));
		wp_enqueue_script('mainjs');
		
		wp_deregister_script( 'l10n' );
	}
}
add_action('init', 'my_script_init');

Initial URL


Initial Description
This code should be added to the functions.php file in the theme's folder. The code will deregister the local copy of jQuery and instead include the google hosted copy. It will also register other scripts.

Initial Title
Include & Register Javascript files in WordPress in functions.php

Initial Tags
javascript, wordpress

Initial Language
PHP